Validate different controls on different buttons in a same page


Normally developers have a requirement to validate different controls on different buttons in this situation developers face problem when user click on any button all validation will be fire which are not belongs to that button functionality.

ASP.NET V2 introduces a new “ValidationGroup” property on validation and input controls that now makes this possible. This allows page developers to group different controls together for more granular validation behavior.

For example two groups – a “Group1” and a “Group2” of validators. There are then two buttons on the page – when button1 is clicked, the first group of validators will fire. When button2 is clicked, the second group of validators will fire. Postback will be blocked client-side by default if the validation fails:


Client Side Validation



     <form runat=“server”>

          <asp:textbox id=“TextBox1” runat=“server”/>

          <asp:requiredfieldvalidator ValidationGroup=“Group1” ErrorText=“Need to Fill in Value!” ControlToValidate=“TextBox1” runat=“server”/>

            <asp:textbox id=“TextBox2” runat=“server”/>

            <asp:requiredfieldvalidator ValidationGroup=“Group2” ErrorText=“Need to Fill in Value!” ControlToValidate=“TextBox2” runat=“server”/>

            <asp:button text=“Group1” ValidationGroup=“Group1” runat=“server”/>

            <asp:button text=“Group2” ValidationGroup=“Group2” runat=“server”/>






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.


Server Side Validation

If you want to validate on server side just call page.Validate(“Group Name”) on button click event it will automatically call respective group.You don’t have need to define specific group on html code.


<form id="form1" runat="server">


<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"  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"  Text="Submit 2" />





protected void Submit1_Click(object sender, EventArgs e)







        // Continue




protected void Submit2_Click(object sender, EventArgs e)







        // Continue



This entry was posted in Tips. Bookmark the permalink.

One Response to Validate different controls on different buttons in a same page

  1. Zeeshan Umar says:

    Really nice article, keep writing like this.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s