How easily to create a required validator requiredfieldvalidator for CheckBox control in asp.net 2.0

Challenge:

Asp.NET I tried to add a CheckBox control to the registration page in my newly implemented open source YetAnotherForum asp.net application. And this CheckBox is for Terms and Conditions agreement, so it needs to be checked before the users submit registration form. It looked like the easy way was to use a RequiredFieldValidator to validate the CheckBox control. But unfortunately this RequiredFieldValidator validation Web control does not work with the CheckBox (or CheckBoxList) Web controls. After I set its ControlToValidate property to the ID of the CheckBox, the page threw an HttpException, saying:

"Control ‘controlID’ referenced by the ControlToValidate property of ‘validationControlID’ cannot be validated." 

Ok, I can perform the validation check in the postback code or replace the RequiredFieldValidator with CustomValidator control. But this is for a constantly updated open source application, I did not want to change too much source code in case I will get overwritten in the next release. So, I need a quick and dirty way to do this.


Solutions:

Thanks to guys from 4guysfromrolla.com, I found my quickest way to do this by adding a custom, compiled validation server control. and even better, this server control can optionally enable or disable the submit button web control (like the image shows below).

Here is the steps for you to follow if you need to do the same.

1), Simple and Quick way

  1. Download this server control source code from here (in zip format).
  2. Unzip all file to your computer, and make sure you have a DLL file named skmValidators.dll (under the directory of /skmValidators/skmValidators/bin/Debug), the copy and paste to your applicaiton’s bin folder.
  3. Open your Visual Studio 2005, Right click on the Toolbox, select Choose Items… and browse to skmValidators.dll you just copied. Then from the list to add CheckBoxListValidator and CheckBoxValidator to your ToolBox (your Toolbox looks like the figure below). Next time, you need to use them, just drag and drop to your asp.net web form.

More advanced way:

If you are not sure what the server control for or just want to know more how they work, then I suggest you spend a little time on the reading. Here is the link to article which explains and gives you more information. Have fun in programming.

Reference link: Creating Validator Controls for the CheckBox and CheckBoxList

  • Share/Bookmark
Tags:

Related posts

3 Responses to “How easily to create a required validator requiredfieldvalidator for CheckBox control in asp.net 2.0”


Leave a Reply