10

我正在尝试required在我的 .NET 4.5 webforms 应用程序中的表单字段上使用新属性。

但是,由于页面上的所有控件只有一个<form>(webforms 样式),因此浏览器不知道在按下提交按钮时要验证哪些字段。

下面附上了具有登录功能和页面搜索的页面的剥离版本。

当我单击搜索按钮时,我的浏览器告诉我必须先填写用户名和密码字段。当我尝试登录时,它告诉我必须在搜索字段中输入一些文本。因为所有字段都是同一个<form>标签的孩子。

其他人有这个问题或想出了解决方案吗?我想使用 HTML5 属性进行表单验证,没有 javascript 解决方案。

<!DOCTYPE html>

<html>
<head>
    <title>Title</title>
</head>
<body>
  <form method="post" runat="server" id="mainform">


    <%// Search %>
    <asp:Panel runat="server" DefaultButton="searchButton" >

      <asp:TextBox runat="server" ID="searchQuery" ClientIDMode="Static" required="required" />
      <asp:Button runat="server" ID="searchButton" ClientIDMode="Static" Text="Search"/>

    </asp:Panel>


    <%// Login %>
    <asp:Panel runat="server" DefaultButton="signInButton">

      <label for="inputusername">Username</label>
      <asp:TextBox runat="server" ClientIDMode="Static" ID="inputusername" required="required" />

      <label for="inputpassword">Password</label>
      <asp:TextBox runat="server" TextMode="Password" ClientIDMode="Static" ID="inputpassword" required="required" />

      <asp:Button ID="signInButton" runat="server" />

    </asp:Panel>

  </form>
</body>
</html>

</html>
4

3 回答 3

5

请试试这个..它对我有用..

<asp:TextBox runat="server" 
             ClientIDMode="Static" 
             ID="inputusername"                    
             required />
于 2015-03-25T09:42:00.033 回答
0

将 novalidate 属性添加到“搜索”按钮。就这么容易。

http://www.w3schools.com/tags/att_form_novalidate.asp

于 2016-12-13T14:07:15.730 回答
-3

由于 ASP.Net Web 窗体编程模型不允许多个 HTML 标记,因此您无法利用这一点。

于 2014-06-29T15:02:21.980 回答