Validation group in ASP. Net data verification

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.