Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ASP.NET & Web Forms » Selective Validation in ASP.Net 2.0

Selective Validation in ASP.Net 2.0

In this article, we will explore the Validation Groups feature introduced in ASP.Net 2.0.

Author Rank:
Technologies: ASP.NET 1.0,Visual C# .NET
Total downloads :
Total page views :  22989
Rating :
 1/5
This article has been rated :  1 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
Become a Sponsor



Validation Groups in ASP.Net 2.0

 

Validation Controls are used to validate user input entered in a web form.

 

Types of Validation Controls:

 

  • RequiredFieldValidator Control: Ensures that the user provides a value for the specied control
  • CompareValidator Control: Validate a control value against a constant value or against the value of another control.
  • RangeValidator Control: Validate a control value against an upper and lower boundary value.
  • RegularExpressionValidator Control: Validate a control value against a specified expression pattern.
  • CustomValidator Control: Validate a control value using custom validation logic.

 

Validation Groups

 

Validation Group is a new feature introduced in ASP.Net 2.0. This enhancement allows specifying selective partial validation in a web form using validation controls. Each validation group is evaluated separately from other validation groups in the web form.

 

How are Validation Groups specified?

 

You can specify Validation Groups by setting the ValidationGroup Property of Validation Controls to a value. All the Validation Controls with the same value set for the ValidationGroup property are included in the same Validation Group.

 

How do you specify the Validation Group that is to be used for the validation event?

 

Controls that can trigger validation, such as the button control, dropdownlist control etc, have the ValidationGroup property which is used to identify the set of validation controls to evaluate, if this control activates a PostBack.

 

How are Validation Groups evaluated?


When a Page is posted back, the IsValid property of the Page is set based on the evaluation of the validation controls that are included in the ValidationGroup which is specified in the control causing the Postback.

 

Different controls can thus be setup to invoke a different set of validations for the web form.

 

Case Sensitive

 

Note that the ValidationGroup property is case-sensitive.

 

Sample: Using Validation Groups

 

In this sample, the RequiredFieldValidator Controls setup for the Login and Password text fields are included in the "VGrpAuthenticate" validation group and the RequiredFieldValidator Control setup for the NickName field is included in the "VGrpAnon" validation group.

 

The Login button control's ValidationGroup is set to "VGrpAuthenticate" and clicking the Login button results in evaluation of only the RequiredFieldValidator controls setup for the login and password text boxes. The RequiredFieldValidator control for the NickName field is not included in this ValidationGroup

 

Sample Code: Using Validation Groups

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

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

<center><H3> Validation Groups Sample</H3>

<div style="width:60%;border-bottom:blue thin solid;border-top:blue thin solid;border-left:blue thin solid;border-right:blue thin solid">

&nbsp;<br />

<center>

<table border="0">

<tr>

<td style="width: 100px; height: 23px">

User ID</td>

<td style="width: 215px; height: 23px">

<asp:TextBox ID="txtLogin" runat="server"></asp:TextBox>

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

ErrorMessage="User ID is required" ValidationGroup="VGrpAuthenticate">*</asp:RequiredFieldValidator></td>

</tr>

<tr>

<td style="width: 100px; height: 23px">

Password</td>

<td style="width: 215px; height: 23px">

<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>

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

ErrorMessage="Password cannot be blank" ValidationGroup="VGrpAuthenticate">*</asp:RequiredFieldValidator></td>

</tr>

<tr>

<td style="width: 100px; height: 21px">

NickName</td>

<td style="width: 215px; height: 21px">

<asp:TextBox ID="txtNickName" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtNickName"

ErrorMessage="NickName is required" ValidationGroup="VGrpAnon">*</asp:RequiredFieldValidator></td>

</tr>

<tr>

<td style="height: 21px" colspan="2">

<br />

<asp:Button ID="btnLoginAuth" runat="server" Text="Login" ValidationGroup="VGrpAuthenticate" />

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

<asp:LinkButton ID="LinkButton1" runat="server" ValidationGroup="VGrpAnon">Login as Guest</asp:LinkButton><br />

</td>

</tr>

</table>

</center>

 

</div>

<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"

ShowSummary="False" ValidationGroup="VGrpAuthenticate" />

<asp:ValidationSummary ID="ValidationSummary2" runat="server" ValidationGroup="VGrpAnon" />

</center>

</form>

</body>

</html> 

 

Image: Using Validation Groups

 

 

Image: Using Validation Groups: "Login" button is clicked

 

 

Image: Using Validation Groups: "Login as Guest" link is clicked

 

 

Programatic Validation

 

To programmatically validate based on selective groups, you can call the Page.Validate method overload which accepts a string specifying the ValidationGroup to validate.

 

ValidationSummary Control

 

The validation summary control also contains a ValidationGroup property. The validation summary control displays the summary results of the validation for a validation group if the ValidationGroup specified for the Validation Control is being evaluated and has validation errors.

 

Multiple ValidationSummary controls can be included in the web form, to specify different ValidationGroups and these validation summary controls can be individually configured.

 

Conclusion

 

In this article, we examined the use of the Validation Group feature. This enhancement in ASP.Net 2.0 allows for handling real life validation scenarios, which were not very easily configurable in previous versions.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Dipal Choksi
Dipal Choksi has over 10 years of industry experience in team-effort projects and also as an individual contributor. She has been working on the .Net platform since the beta releases of .Net 1.0.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Powerful ASP.NET Hosting w/ NO Setup Fees. Click Here!
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved