Validation Groups in ASP.NET 2.0

ASP.Net 2.0 supports validation groups, which is a new feature that is included in this version. The validation groups help you to group the controls in a single page and you can have separate submit buttons for each group, so that you can submit each group individually. Each group can have separate validation controls.



When you submit a group of values from a group, the validation controls pertaining to that group alone is executed. Submission of a particular group of controls will not execute the validation controls of another group. This helps you to group your controls and have separate validation controls for each group.

In the earlier version of ASP.Net, if you have multiple forms in an .aspx page, submission of that page to the server causes validation of all the controls in the page, including the form which is not required to submit values. This is a main drawback of the validation controls in the earlier version of the .Net framework. This prevented developers to use multiple form submission from a single page. This drawback is overcome in ASP.Net 2.0 using Validation groups.

Grouping of controls is achieved in ASP.Net 2.0 by modifying the properties of the form controls, button controls and validation controls to have a additional property called the ValidationGroup. Controls in different validation groups are validated separately.

Consider a scenario where there is a webform with groups of textbox controls. Each group of textbox controls are there to get input values like Personal details of an employee, Experience details of an Employee, and Educational details of the Employee. Each of these three groups can be submitted separately to the server and each of them can have its own validation controls. Let us say that you have RequiredField Validator for all the controls of each group. If you do not enter any value for a textbox control in the Personal details group, the RequiredField validator for that particular group alone is fired, even if you have not entered any value for the other group controls. Similarly validation controls of the other groups are fired, when you try to submit each of that group.

The code given below is of the body of the .aspx page. This code explains clearly the usage of the validation group in a webform.

<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Runat="server" ValidationGroup="First"></asp:TextBox>

<asp:TextBox ID="TextBox2" Runat="server" ValidationGroup="First"></asp:TextBox><br />

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" ValidationGroup="First"
ErrorMessage="TextBox1 should not be blank" ControlToValidate="TextBox1">

</asp:RequiredFieldValidator>
<asp:Button ID="Submit1" Runat="server" ValidationGroup="First" Text="Submit 1" />

<br />
<br />

<asp:TextBox ID="TextBox3" Runat="server" ValidationGroup="Second"></asp:TextBox>

<asp:TextBox ID="TextBox4" Runat="server" ValidationGroup="Second"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" ErrorMessage=" TextBox3 should not be blank"
ControlToValidate="TextBox3" ValidationGroup="Second">
</asp:RequiredFieldValidator>

<asp:Button ID="Submit2" Runat="server" ValidationGroup="Second" Text="Submit 2" />

</div>
</form>
</body>

The code given above has two validation groups. The first validation groups is identified as “First” and the second validation group is identified as “Second”. Each validation group has two textboxes, a RequiredField validator and a button. If you look at the code you might see that each control has a property called “ValidationGroup” and its value is set to the name of the validation group to which that control belongs.

Clicking the “Submit1” button initiates the validation of the first group and throws an error message if the TextBox1 is left blank. Since this button belongs to the ‘First’ validation group, it initiates the validaton controls that belongs to that group. Similarly clicking the ‘Submit2’ button throws an error message if the TextBox3 is left blank. Thus the validation groups help in grouping the controls in a single web page allowing you to have separate validations for different controls while allowing you to submit values in a particular group.





“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.