CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Validation group in ASP.Net 2.0

By Satheesh Babu
Posted On Feb 21, 2008
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: ASP.Net
Print this article.

Validation group in ASP.Net 2.0

ASP.Net 2.0 Validation control has some new features that make the developer life easier. One of the such new feature is the “Validation Groups” in ASP.Net 2.0 validation control. In ASP.Net 1.X days, the validation controls used to fire for all the actions that is causing post back i.e. it fires for every button click,etc. This is a bit drawback in ASP.Net 1.X. To prevent this, we have to make the “Causes Validation” property of other buttons to false so that validation is not triggered for that button. Also, ASP.Net 1.x lacks in providing a feature to make the validation controls applied to a group of controls. For example, if we have a form like below,

 

 

On clicking of “Save Name”, it should fire the “Required Field validator” of First Name and Last Name fields only. In the same way, on clicking “Save Address” it should fire the “Required Field validator” only for Address1 and Address2 fields. But in 1.x, when we try to click any of the above button it will fire all the validation controls on the page, like below figure. Also, there is no direct way to prevent this.

 

Here comes the use of new feature in ASP.Net 2.0 Validation controls called Validation groups. In ASP.Net 2.0, we can see a property called ValidationGroup for every controls that can be validated. We need to set the same validationgroup name for all the controls that needs to be grouped under a same group.

 

 




So, our requirement can be achieved by the validation group property in ASP.Net 2.0. The ASPX page will like,

 

 

<tr>

  <td style="width: 100px">

  First Name</td>

  <td style="width: 100px">

  <asp:TextBox ID="txtFirstName" runat="server" ValidationGroup="Name_Group"></asp:TextBox></td>

</tr>

<tr>

  <td style="width: 100px">

  </td>

  td style="width: 100px">

  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtFirstName"

  ErrorMessage="Enter First Name" ValidationGroup="Name_Group" Width="149px"></asp:RequiredFieldValidator></td>

</tr>

<tr>

   <td style="width: 100px">

   Last Name</td>

   <td style="width: 100px">

   <asp:TextBox ID="txtLastName" runat="server" ValidationGroup="Name_group"></asp:TextBox></td>

</tr>

<tr>

   <td style="width: 100px">

   </td>

   <td style="width: 100px">

   <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtLastName"

   ErrorMessage="Enter Last Name" ValidationGroup="Name_Group"Width="152px"></asp:RequiredFieldValidator></td>

</tr>

<tr>

  <td style="width: 100px">

  <td style="width: 100px">

  <asp:Button ID="btnSaveName" runat="server" Text="Save Name" ValidationGroup="Name_Group" /></td>

</tr>

 

 

Now,When I click “Save name” the output will be like,

 

And when I click “Save Address” the out put will be like

Note:

The value for Validation group is case sensitive.

You can download the source here.

 

Download source:

Download
Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments