带有.NET webforms的HTML5 required =“”属性

时间:2013-03-15 14:09:35

标签: .net html5 webforms

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

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

下面附有一个带有登录功能和页面搜索功能的页面剥离版本。

当我点击“搜索”按钮时,我的浏览器告诉我必须先填写用户名和密码字段。当我尝试登录时,它告诉我必须在搜索字段中输入一些文本。因为所有字段都是具有相同<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>

3 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:0)

将“无效”属性添加到“搜索”按钮。就这么简单。

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

答案 2 :(得分:-3)

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