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 participant section of the page. this article will explain some circumstances where validating a special section of page will be required and how to accomplish it.
Why validating a participant 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 particle only ular 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 participant ular 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 participant section is clicked validation for other sections shocould not happen.
Solution
The solution for by passing validation on server button click is fairly easy. All we have to do is setCausesvalidationProperty of the button control to false. OnceCausesvalidationProperty 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 = "Your cancel" runat = "server" text = "cancel"Causesvalidation= "False"> </ASP: button>
This can also be done in runtime using the following code
Optional cancel. causesvalidation = false;
The solution for validating only participates 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 shocould be completed MED. setting this variable to false will 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 participant validation control we can useValidatorenableFunction 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 particle 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.AttributeS. 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 particle validators on the server side the following code has to be added to the button click event
Validationcontrolname. isvalid = true;