Dot Net Matters
Navigation for Dot Net Matters - Selective Form Validation Using ASP.NET Validation Controls
Content
Sidebar
Footer
June 7, 2007
@ 02:49 PM
Comments [0]
Selective Form Validation Using ASP.NET Validation Controls
ASP.Net comes in with a set of validation controls that automatically validate the data entered by a user. Though this validation controls are very powerful and easy to use, they have a small draw back in the sense that they require the entire page to be valid before it's submitted back to the server. There is no direct way to validate only a particular section of the page. This article will explain some circumstances where validating a particular section of page will be required and how to accomplish it.
Why validating a particular section of page is needed?
While validation controls is a great tool for validating user input before data is being submitted there are some situations where we might either want the data to be submitted without validation or we want to validate only particular fields in the form.
A typical example for when we may want the data to be passed through without validation is when a page has both submit and cancels server buttons. When the user clicks the submit button the data has to be validated whereas when the user clicks the cancel post back has to happen without any validation.
An example for when we may want to validate only particular fields of a page is a form which is divided into multiple sections with a submit button in each section. When a submit button for a particular section is clicked validation for other sections should not happen.
Solution
The solution for by passing validation on server button click is fairly easy. All we have to do is set the
CausesValidation
property of the button control to false. Once the
CausesValidation
property is set to false no validation will happen both on the client side and server side.
The syntax for doing it in the design time is
<asp:button id="cmdCancel" runat="server" Text="Cancel"
CausesValidation
="False"></asp:button>
This can also be done in runtime using the following code
cmdCancel.CausesValidation = false;
The solution for validating only particular fields requires few lines of JavaScript code and understanding of the Client side API provided by the validation controls. Following table lists the functions and variables provided by the client side API
Name
Description
Page_IsValid
A Boolean variable which indicates whether the page is valid.
Page_Validators
Array of all of the validators in the current page.
Page_ValidationActive
A Boolean variable which indicates whether validation should be performed. Setting this variable to False will to turn off validation.
Isvalid
This is a property of the client validator indicating whether it is valid.
ValidatorEnable(val, enable)
Enables or disables the client validator passed as argument.
To disable a particular validation control we can use the
ValidatorEnable
function as shown in the script below
<script language="javascript">
ValidatorEnable(nameofvlaidationcontrol, false)
</script>
To disable all the validation control we need to loop through Page_Validators array and disable each validator as shown in script below
<script language="javascript">
for(i=0;i< Page_Validators.length;i++)
{
ValidatorEnable(Page_Validators[i], false)
}
</script>
In order to enable validation only for particular set of controls when a submit button is clicked we need to combine the above two scripts and call it from the client click event of the button as shown in sample below
<script language="javascript">
function enableRegionValidators()
{
for(i=0;i< Page_Validators.length;i++)
{
ValidatorEnable(Page_Validators[i], false)
}
ValidatorEnable(rvRegion, true)
}
</script>
Attaching the function to the client click event of a submit button
cmdRegion.
Attribute
s.Add("onclick","enableRegionValidators();");
When the cmdRegion submit button is clicked all the other validators will be disabled and only the validator named rvRegion will be enabled. If the validation of rvRegion is successful then the page will be submitted. The code we have used so far will disable the validatiors only on the client side, so the validation will still happen on the server side and error messages will be shown. To disable particular validators on the server side the following code has to be added to the button click event
validationcontrolname.IsValid=true;
Categories:
Asp.Net/Web Services
Related posts:
Upcoming ASP.NET Releases in April
New Article Compares LINQ to Similar Technologies
ASP.NET Developer Offers New TreeView Product
ASP to ASP.NET Migration Strategy
Instrument and Monitor Your ASP.NET Apps Using WMI and MOM 2005
MSDN Webcast: Advanced Topics for Visual Studio Tools for the Microsoft Office System
« .NET Micro Framework Demonstrated for Em...
|
Home
|
Adobe releases Beta tools for Flex and A... »
Comments are closed.
Search
RSS/Subscribe
Subscribe (RSS)
Latest Posts
Adobe Opens Up Flash
Microsoft Takes SharePoint and Exchange Servers Online
SQL Server 2008 Late
PDC Is Back On
Microsoft Releases Entity Framework Beta 3
Next SQL Server CTP Coming Soon
Microsoft Debuts Search Server 2008 Express
Microsoft Offers IE7 to all, Pirates Included
Categories
.Net Framework (9)
asp.net Validators (1)
Asp.Net/Web Services (22)
Atlas/AJAX (6)
Mono (1)
Other (29)
Security (9)
Sharepoint (14)
SilverLight (2)
SQL Server (21)
VB.Net/C#/Visual Studio (16)
Web Development (52)
Windows 2008 (1)
Archives
May, 2008 (1)
March, 2008 (1)
January, 2008 (1)
December, 2007 (2)
November, 2007 (2)
October, 2007 (3)
September, 2007 (1)
July, 2007 (2)
June, 2007 (10)
May, 2007 (3)
April, 2007 (2)
January, 2007 (1)
December, 2006 (1)
October, 2006 (2)
July, 2006 (1)
June, 2006 (2)
May, 2006 (2)
April, 2006 (1)
March, 2006 (2)
February, 2006 (7)
January, 2006 (8)
December, 2005 (3)
November, 2005 (6)
October, 2005 (8)
September, 2005 (8)
August, 2005 (12)
July, 2005 (11)
June, 2005 (13)
May, 2005 (25)
April, 2005 (40)
March, 2005 (1)
BlogRoll
Alt.Net Podcast
Dot Net Rocks TV
Power Scripting Podcast
Scott Hanselman
The Technophilez
Windows Admin.Info
Categories
.Net Framework
asp.net Validators
Asp.Net/Web Services
Atlas/AJAX
Mono
Other
Security
Sharepoint
SilverLight
SQL Server
VB.Net/C#/Visual Studio
Web Development
Windows 2008
Sponsors