Validation groups allow you to organize validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page.
You create a validation group by setting the ValidationGroup property to the same name (a string) for all the controls you want to group. You can assign any name to a validation group, but you must use the same name for all members of the group.
During postback, the LoginForm
is clicked, then the IsValid property will return true if all validation controls whose ValidationGroup property is set to LoginForm
are valid. Other controls such as a
To validate programmatically, you can call the
The following code example demonstrates how to use the ValidationGroup property to specify the controls to validate when a PersonalInfoGroup
validation group and the RequiredFieldValidator control for the third text box is in the LocationInfoGroup
validation group. When Button1
is clicked, only the controls in validation group PersonalInfoGroup
are validated. When Button2
is clicked, only the control in validation group LocationInfoGroup
is validated.
Visual BasicВ | Copy Code |
---|---|
<%@ page language="VB" %> <html> <head runat="server"> <title>Button.ValidationGroup Example</title> </head> <body> <form runat="server"> <h3>Button.ValidationGroup Example</h3> <asp:label id="NameLabel" text="Enter your name:" runat=Server> </asp:label>   <asp:textbox id="NameTextBox" runat=Server> </asp:textbox>   <asp:requiredfieldvalidator id="RequiredFieldValidator1" controltovalidate="NameTextBox" validationgroup="PersonalInfoGroup" errormessage="Enter your name." runat=Server> </asp:requiredfieldvalidator> <br /><br /> <asp:label id="AgeLabel" text="Enter your age:" runat=Server> </asp:label>   <asp:textbox id="AgeTextbox" runat=Server> </asp:textbox>   <asp:requiredfieldvalidator id="RequiredFieldValidator2" controltovalidate="AgeTextBox" validationgroup="PersonalInfoGroup" errormessage="Enter your age." runat=Server> </asp:requiredfieldvalidator> <br /><br /> <!--When Button1 is clicked, only validation controls that are a part of PersonalInfoGroup are validated.--> <asp:button id="Button1" text="Validate" causesvalidation=true validationgroup="PersonalInfoGroup" runat=Server /> <br /><br /> <asp:label id="CityLabel" text="Enter your city of residence:" runat=Server> </asp:label>   <asp:textbox id="CityTextbox" runat=Server> </asp:textbox>   <asp:requiredfieldvalidator id="RequiredFieldValidator3" controltovalidate="CityTextBox" validationgroup="LocationInfoGroup" errormessage="Enter a city name." runat=Server> </asp:requiredfieldvalidator> <br /><br /> <!--When Button2 is clicked, only validation controls that are a part of LocationInfoGroup are validated.--> <asp:button id="Button2" text="Validate" causesvalidation=true validationgroup="LocationInfoGroup" runat=Server /> </form> </body> </html> |
C#В | Copy Code |
---|---|
<%@ page language="C#" %> <html> <head id="Head1" runat="server"> <title>Button.ValidationGroup Example</title> </head> <body> <form id="Form1" runat="server"> <h3>Button.ValidationGroup Example</h3> <asp:label id="NameLabel" text="Enter your name:" runat=Server> </asp:label>   <asp:textbox id="NameTextBox" runat=Server> </asp:textbox>   <asp:requiredfieldvalidator id="RequiredFieldValidator1" controltovalidate="NameTextBox" validationgroup="PersonalInfoGroup" errormessage="Enter your name." runat=Server> </asp:requiredfieldvalidator> <br /><br /> <asp:label id="AgeLabel" text="Enter your age:" runat=Server> </asp:label>   <asp:textbox id="AgeTextbox" runat=Server> </asp:textbox>   <asp:requiredfieldvalidator id="RequiredFieldValidator2" controltovalidate="AgeTextBox" validationgroup="PersonalInfoGroup" errormessage="Enter your age." runat=Server> </asp:requiredfieldvalidator> <br /><br /> <!--When Button1 is clicked, only validation controls that are a part of PersonalInfoGroup are validated.--> <asp:button id="Button1" text="Validate" causesvalidation=true validationgroup="PersonalInfoGroup" runat=Server /> <br /><br /> <asp:label id="CityLabel" text="Enter your city of residence:" runat=Server> </asp:label>   <asp:textbox id="CityTextbox" runat=Server> </asp:textbox>   <asp:requiredfieldvalidator id="RequiredFieldValidator3" controltovalidate="CityTextBox" validationgroup="LocationInfoGroup" errormessage="Enter a city name." runat=Server> </asp:requiredfieldvalidator> <br /><br /> <!--When Button2 is clicked, only validation controls that are a part of LocationInfoGroup are validated.--> <asp:button id="Button2" text="Validate" causesvalidation=true validationgroup="LocationInfoGroup" runat=Server /> </form> </body> </html> |