Data verification is used to prevent unnecessary errors on the server caused by incorrect data input or missing required data. ASP. NET has a verification control to facilitate data verification, but sometimes this or that problem occurs. I have encountered one today. Let's talk about the verification control. <asp: textBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: RequiredFieldValidator ID = "RequiredFieldValidator1" ControlToValidate = "TextBox1" runat = "server" ErrorMessage = "RequiredFieldValidator"> </asp: RequiredFieldValidator>
<Asp: Button ID = "Button1" runat = "server" Text = "Button1"/>
The above code implements the most basic verification. If you click Button1 without entering the information, the error message will be displayed.
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: RequiredFieldValidator ID = "RequiredFieldValidator1" ControlToValidate = "TextBox1" runat = "server" ErrorMessage = "RequiredFieldValidator"> </asp: RequiredFieldValidator>
<Asp: Button ID = "Button1" runat = "server" Text = "Button1"/>
<Asp: Button ID = "Button2" runat = "server" Text = "Button2"/>
In the above Code, if you click Button1 or Button2 without entering the information, the error message will be displayed. What if I don't want Button2 to trigger verification? Well, add a property CausesValidation = "false" to Button2 as follows:
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: RequiredFieldValidator ID = "RequiredFieldValidator1" ControlToValidate = "TextBox1" runat = "server" ErrorMessage = "RequiredFieldValidator"> </asp: RequiredFieldValidator>
<Asp: Button ID = "Button1" runat = "server" Text = "Button1"/>
<Asp: Button ID = "Button2" runat = "server" Text = "Button2" CausesValidation = "false"/>
In this way, if the user clicks Button2 ErrorMessage without entering the information, the message will not appear.
But sometimes we need to verify different information on the same page. You certainly cannot add the CausesValidation = "false" button. What we need to add is the ValidationGroup attribute.
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: RequiredFieldValidator ID = "required" ControlToValidate = "TextBox1" ValidationGroup = "basicInfo" runat = "server" ErrorMessage = "error form textbox1"> </asp: RequiredFieldValidator>
<Asp: Button ID = "Button1" ValidationGroup = "basicInfo" runat = "server" Text = "Button1"/>
<Asp: TextBox ID = "TextBox2" runat = "server"> </asp: TextBox>
<Asp: RequiredFieldValidator ID = "required" ControlToValidate = "TextBox2" ValidationGroup = "moreInof" runat = "server" ErrorMessage = "error form textbox2"> </asp: RequiredFieldValidator>
<Asp: Button ID = "Button2" ValidationGroup = "moreInof" runat = "server" Text = "Button2"/>
In this way, you click the Button to only trigger verification on a specific button, without affecting other buttons.
Conclusion: You can use a verification group to group verification controls on the page. Each verification group can be verified, regardless of other verification groups on the same page.
You can create a verification group by setting the ValidationGroup attribute of all controls to be grouped to the Same Name (string. You can assign any name to the verification group, but you must use the same name for all members of the group.