Thursday, 24 March 2011

Asp.Net Validators: Useful Client-Side Properties & Methods

Hi,
This post expose few useful property and methods of Asp.Net Validators at client-side in order to interact with them when needed using JavaScript.

1. Page_IsValid
Use: This is a property exposed by Asp.Net validators in order to check the status of validations performed. Similarly to Page.IsValid property at server-side.
Value: 
True - If all Validations are successful
False - If any one of the Validation fails

2.ValidatorEnable(val, enable)
Parameters:
val:  object, Validator Control Object
enable: bool, Enables/Disables Validation control
How To Call:
ValidatorEnable(document.getElementById('validatorid')); //enable will be true - default or
ValidatorEnable(document.getElementById('validatorid'), true);
Use: To enable/disable Validation Control and to fire Validation of a particular Validator control and it updates Page_IsValid property of page.
It performs 3 things
1. Enable/Disable Validation control (based on parameter, enable)
2. Fires Validation
3. Updates Page_IsValid property to reflect result of Validation performed.
If you want to perform only Step 2 (Fires Validation), you can use below function

3. ValidatorValidate(val, validationGroup, event)
Parameters:
val: object, Validator Control Object
validationGroup: string, Group of validation control
event: event, Event caused Validation to fire
How to call: 
ValidatorValidate(document.getElementById('validatorid')); or
ValidatorValidate(document.getElementById('validatorid'), 'grpAdd'); or
ValidatorValidate(document.getElementById('validatorid'), 'grpAdd', event);
Use: To fire Validation of a particular Validator control but it doesn't updates Page_IsValid property of page.

4.ValidationSummaryOnSubmit(validationGroup)
Parameters:
validationGroup: string, Used to show Validation Summary of this group (if not supplied it will work on all ValidationSummary in page)
How To Call:
ValidationSummaryOnSubmit(); or
ValidationSummaryOnSubmit('grpAdd');
Use: It is used to show ValidationSummary of particular group(if specified). Suppose you have called  ValidatorEnable of a ValidationControl and want to show ValidationSummary on failure, you can use function ValidationSummaryOnSubmit to achieve this.
http://abhijit-j-shetty.blogspot.com/2011/03/aspnet-validation-summary-how-to-show.html




No comments:

Post a Comment