Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 required="" attribute with .NET webforms

I'm trying to use the new required attribute on form fields in my .NET 4.5 webforms application.

However, since there is only one <form> surrounding all the controls on the page (webforms style), the browser does not know which fields to be validated when a submit button is pressed.

A stripped version of a page with both login functionality and a page search is attached below.

When I click the Search button my browser tell me that i have to fill in the username and password fields first. And when I try to login it tells me I have to enter some text in the search field. Because all of the fields are children of the same <form> tag.

Any one else had this problem or came up with a solution? I would like to use the HTML5 attributes for form validation, no javascript solution.

<!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>
like image 804
chilly Avatar asked Mar 15 '13 14:03

chilly


1 Answers

Try this please.. It worked for me..

<asp:TextBox runat="server" 
             ClientIDMode="Static" 
             ID="inputusername"                    
             required />
like image 186
Ahsan Raza Avatar answered Oct 08 '22 18:10

Ahsan Raza