Friday, September 30, 2011

Validation Groups in ASP.NET 2.0


Validation groups is a new feature of asp.net 2.0.Validation groups help you to group the controls in a page and you can have separate
submit buttons for each group.When you submit a group of values from a group,the validation controls pertaining to that group alone is executed.

Here is a example how to use validation group.
On clicking of Save Button,it should fire the Required Field validator of Name is required fields only. In the same way,on clicking Save Password it should fire
the Required Field validator only for Password is required fields.

For Group1
<asp:TextBox ID="txtusername" runat="server" ValidationGroup="Group1"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUname" runat="server" ControlToValidate="txtusername"
ErrorMessage="Name is required." ValidationGroup="Group1">*</asp:RequiredFieldValidator>
<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="Group1" />

For Group2
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" ValidationGroup="Group2></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvpass" runat="server" ControlToValidate="txtPassword"
ErrorMessage="Password is required." ValidationGroup="Group2">*</asp:RequiredFieldValidator>
<asp:Button ID="btnSavePass" runat="server" Text="Save Password" ValidationGroup="Group2" />

Thanks & Regards