Dot-Net

帶有 .NET 網路表單的 HTML5 required=’’ 屬性

  • December 13, 2016

我正在嘗試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>

請試試這個..它對我有用..

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

將 novalidate 屬性添加到“搜尋”按鈕。就這麼容易。

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

引用自:https://stackoverflow.com/questions/15434535