Sometimes we have multiple sections in our web forms for validation. These sections need their separate validation. For example I have two sections - one to insert a category and other one to update existing category name.
For inserting category the text box is txtNewCategory and for updating category text box is txtUpdateCategory. Both have RequiredFieldValidator validation control.
In this case we want that when validation of txtNewCategory happens then txtUpdateCategory should not be validated and vice versa.
This can be achieved by creating Validation Group. Here is the example -
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ValidationGroup="GrpUpdateCategory" ControlToValidate="txtCategoryName" ErrorMessage="Please enter updated category name">*
</asp:RequiredFieldValidator>
For inserting category the text box is txtNewCategory and for updating category text box is txtUpdateCategory. Both have RequiredFieldValidator validation control.
In this case we want that when validation of txtNewCategory happens then txtUpdateCategory should not be validated and vice versa.
This can be achieved by creating Validation Group. Here is the example -
<asp:TextBox ID="txtNewCategory" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator ID="reqFieldCategory" runat="server" ValidationGroup="GrpNewCategory" ControlToValidate="txtNewCategoryName" ErrorMessage="Please enter new category name">
</asp:RequiredFieldValidator>
<asp:Button ID="btnCategoryAdd" runat="server" Text="Add Category" OnClick="btnCategoryAdd_Click" ValidationGroup="GrpNewCategory"/>
<asp:TextBox ID="txtUpdateCategory" runat="server" > </asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ValidationGroup="GrpUpdateCategory" ControlToValidate="txtCategoryName" ErrorMessage="Please enter updated category name">*
</asp:RequiredFieldValidator>
<asp:Button ID="btnCategoryUpdate" runat="server" Text="Add Category" OnClick="btnCategoryUpdate_Click" ValidationGroup="GrpUpdateCategory"/>
In the above markup we create two validation group - GrpNewCategory and GrpUpdateCategory.
GrpNewCategory is associated with txtNewCategory while GrpUpdateCategory is associated with txtUpdateCategory.
Comments
Post a Comment