Tuesday, December 14, 2010

ValidationGroup in ASP.NET

More than often we come across screens with multiple sections that can perform specific operations independently. For example a maintenance screen can be used to add more than one type of a Look Up entry. In such screens where certain control can be grouped together we can use ValidationGroup to perform the validations independently. Without this we might end up not using a validation control on the page and perform all the validations on the server side inside the button click event.

Consider the following screen that is used to Add Vendors and Agents to the system.








Vendor Name and Vendor Email are mandatory for adding Vendor, Agent Name and Agent ID are mandatory for adding Agent, However they are independent tasks by themselves. When I click on Add Agent it validates all control in the screen


Now I have added the ValidationGroup for the textboxes and associated them with the Button and ValidationControl. Now it validates only those controls that are in the validation group










The source code is:
<asp:ValidationSummary runat="server" ValidationGroup="VendorGroup"/>
<h2>Add New Vendor: </h2>
<table cellpadding="0" cellspacing="5" width="80%" border="1">
    <tr>
        <td>Vendor Name:
            <asp:TextBox ID="customerTextBox" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="reqval" runat="server" Text="*" ControlToValidate="customerTextBox"
            ErrorMessage="Enter Vendor Name" ValidationGroup="VendorGroup"></asp:RequiredFieldValidator>
        </td>
        <td>Vendor Email:
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Text="*" ControlToValidate="TextBox1"
            ErrorMessage="Enter Vendor Email" ValidationGroup="VendorGroup"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:Button ID="customerButton" runat="server" Text="Add Vendor" ValidationGroup="VendorGroup"/>
        </td>
    </tr>
</table>
<br />
<br />
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="AgentGroup"/>
<h2>Add New Agent: </h2>
<table cellpadding="0" cellspacing="0" width="80%" border="1">
    <tr>
        <td>Agent Name:
            <asp:TextBox ID="orderIDTextBox" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Text="*" ControlToValidate="orderIDTextBox"
            ErrorMessage="Enter Agent Name" ValidationGroup="AgentGroup"></asp:RequiredFieldValidator>
        </td>
        <td>Agent ID:
            <asp:TextBox ID="customerIDTextBox" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" Text="*" ControlToValidate="customerIDTextBox"
            ErrorMessage="Enter Agent ID" ValidationGroup="AgentGroup"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:Button ID="orderButton" runat="server" Text="Add Agent" ValidationGroup="AgentGroup"/>
        </td>
    </tr>
</table>

As you can see all we have to do is add the ValidationGroup property to the validation controls, button and the Validationsummary control.  We must be cautious to provide a unique Name for the ValidationGroup across the page and use the same name in the button.

There might be scenario when more than one validation group must be validated when the page does the postback. In such cases we can validate the groups separately from the code behind using Page.Validate overload that takes the groupname
Page.Validate("VendorGroup");
Page.Validate("AgentGroup");

Note: Page.Validate() will validate all the validationgroups within the page.

1 comment:

  1. After 3 days of screwing with this and reading countless pages of what I needed to do the

    'Note: Page.Validate() will validate all the validationgroups within the page.'

    that you stuck in prob as an afterthought was what I needed. Thanks for that little tidbit.

    ReplyDelete