When creating a form using an ASP.NET Web Application (.NET Framework) template, I have a TextBox that holds a string of characters and 2 buttons, one to reset the form and one to submit the form... When filling in the form the "reset" button works the same way as the "submit" button, in that the "required" tag is triggered if the textbox is empty when the user clicks the reset button. I can't seem to get the button behaviour to work correctly.
Is there a way to set the "reset" button to not trigger the textbox "required" tag?
Example of my code;
<h3>Account Number</h3>
<asp:TextBox ID="AccountNumber" name="AccountNumber" MaxLength="18" placeholder="2347655454" required="required" runat="server" Width="300px" Height="40px" Font-Size="Large"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Reset Form" Width="300" Height="50px" Font-Bold="True" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" ForeColor="Black" OnClick="reset_Click" />
<br />
<asp:Button ID="Button2" runat="server" Text="Submit Form" Width="300" Height="50px" Font-Bold="True" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" ForeColor="Black" OnClick="Mong_Click" />
CodePudding user response:
Why bother with the required tag? Just put the logic in your button code.
if (Textbox1.Text == "") {
// logic here to tell user they have to enter some data
// you can even use registerscript and pop open a client side
// dialog
return
}
So, since in 99% of cases we assume that some text will be entered, then suffering a post back once in a blue moon should not really matter in the scope of things, should it?
I would just put the logic in the button event code, and not really worry about this all that much.