CustomValidator to validate multiple controls
Is there a way to have the custom validator take the input from two controls, say for example two check boxes and make sure that only one is checked?
Or am I goi开发者_C百科ng about this all wrong, and more importantly if I am could you point me in the right direction?
Even if this is the wrong direction actually I would like to know how to do this in case I run into a situation where it is applicable, that is if it's possible.
Thanks for you help in advance.
If you want to make sure that only one of two checkboxes are checked, you are using the wrong control.
Use a radio button to setup an exclusive selection.
The AJAX Control Toolkit has an exclusive checkbox list. I agree with the need for it, as radio buttons cannot be de-selected like checkboxes can.
If you're looking to roll a custom validator, it sounds like you either do it server side, at which point you can do whatever logic you want in the code behind. Conversely, you can roll your own javascript to enforce it.
I agree with Mitchel that a RadioButton seems more appropriate.
But have you tried hooking up a CompareValidator and set it to validate UnEqual?
I'm also working on a similar problem. The way I worked around this issue, was to create a validator for each of my controls, and to link them to the same method that ensures whatever your validation restrictions are.
protected void validator1_ServerValidate(...){
myValidationMethod(...);
}
protected void validator2_ServerValidate(...){
myValidationMethod(...);
}
It works, but it's pretty ugly IMO
I wrote a blog article about this very thing.
http://coding.infoconex.com/post/2010/10/11/ASPNET-CustomValidator-that-validates-multiple-controls-using-both-Server-Side-and-Client-Side-scripting
Combines both Client Side and Server Side validation and re-validates
精彩评论