I place a checkboxlist control into my page and I’d like to validate it. I mean, my user have to select at least one of the given choices, unfortunately, using customer validator or required field validator control can’t validate the said control. How am I suppose to handle it? Please help me.
The Custom Validator is what you need to use.
Just do not set the ControlToValidate property.
Set the OnServerValidate=“MyCustomValidate”, then do the checks to see if the user selected any of your checkboxes.
eg.
public void MyCustomValidate(object sender , ServerValidateEventArgs args)
{
if(userhascheckedsomething)
args.IsValid = True;
else
args.IsValid = False
}
Just make sure you set the CausesValidation property to True on the CheckBoxlist
Thank you so much…
Or even better, use the CheckBox and CheckBoxList Validator
Wouldn’t that be overkill? I mean, with a custom validator you can check all controls, including checkbox, at the same time.
if(checkbox1.checked)
{
//They agreed
}
I would rather check them all in one area, instead of writing extra code you would not need. Just my 2 cents.
With that argument, you might as well use a custom validator all the time, so you don’t have to use other validators
You don’t need to write any code, download the dll, drop it in your bin directory, Add it to your Toolbox, and you can drag and drop the validator just like any other validator, the properties are accessble in the properties window. You nee to write more code using a custom validator. And not only that. This validator, like the other validators, work on server and client side, without writing a single line of code (VB/c# and/or jacascript)
Honestly, I think the Custom Validator should be all to end all. Drop the Custom Validator on the form, wire it to a function, validate all controls with every method in the book (check for empty, wrong data type, regex, .ect). Build a “error” varible, format it, send it to a literal control. Also, at the same time, add CSS to the form elements if they are incorrect (red line around textbox). Their is so much you can do with a Custom Validator, the possibilitys are endless, but it might not be the best solution in all senerios (RAD). Their is a billion ways to build the same app, to each their own…
The CustomValidator itself is overkill for a lot of scenarios, especially when all you need is a RequiredFieldValidator. There are specific validators for specific purposes, you can also create your own, and then the CustomValidator is the catch-all for everything else.
Cheers,
D.