Friday, April 3, 2009

Disabling asp.net client side validator affects page.IsValid

Sometime we need to disable page validator based on user selection. For example in the following image when user click the save button the three validators are used for client side validation.

image

Now if user clicks on Reset Password then we don't need to disable the client side validators related to the three fileds (old password, password, confirm password). To enable/disable validators we need to use asp.net provided client side method:

ValidatorEnable(validator, enabled);

The first parameter is the validator object itself. The second parameter defines whether the validator will be enabled or disabled. On the click event of Reset Password checkbox we'll call the ValidatorEnable method in javascript. As shown in the following image reset password will disable the client side validator.

 

image

Pretty simple! huh!

Now on the save button event you'll check if the Page.IsValid is true to ensure that client side code is validated properly. But you have disabled the client side validation with javascript. So the Page.IsValid will return false. The solution before calling Page.IsValid ensure that the validators that you disabled on the client side also disabled on server side. so you code will be like:

if(ResetPassword Chekcbox is checked)

{

1. disable validator related to old password.

2. disable validator related to password.

3. Disable validator related to confirm password

}

call Page.IsValid

Now when you'll call the page.Isavalid, you'll get the true as the call of page.isvalid will ignore those controls' that are not enabled. So if you disable a validator from server side then page.Isavalid will ignore whether the validator we validated or not on the client side.

1 comment:

  1. Nice information, I really appreciate the way you presented.Thanks for sharing..

    http://www.w3cvalidation.net/

    ReplyDelete

Note: Only a member of this blog may post a comment.