Scott on Writing

Musings on technical writing...

An ASP.NET 2.0 Validator Web Control for CheckBoxes and CheckBoxLists

ASP.NET provides a variety of validation Web controls that can be used to validate a user's form field inputs. Unfortunately, the validation Web controls do not work with the CheckBox or CheckBoxList Web controls. If you set a validation control's ControlToValidate property to the ID of a CheckBox or CheckBoxList, the page will throw an HttpException, stating: "Control 'controlID' referenced by the ControlToValidate property of 'validationControlID' cannot be validated."

There may be times, however, when you need to provide validation for a CheckBox or CheckBoxList. In fact, a rather popular article on 4Guys is Creating a Validation Control for CheckBoxLists by Cenk Civici and yours truly, which shows how to create a CheckBoxList validation control to ensure that at least one checkbox from the list is selected. Similarly, I recently got an email from a reader of my Working with Data in ASP.NET 2.0 tutorial series asking about validating a CheckBox being checked. For example, many Web pages with Terms of Service include a CheckBox titled "I agree to the above terms" that must be checked before continuing.

To provide such validation, we have three choices:

  1. Forgo any sort of validation Web control semantics and perform the validation check using code on postback. The downside of this is that it breaks from the standard validation control metaphor and requires extra effort to include client-side validation.
  2. Use the CustomValidator control and define our own server-side and client-side validation logic. The benefit of this approach is that it adheres to the validation control metaphor; however, the validation logic is tightly bound to the ASP.NET page, meaning that the server-side and client-side validation must be replicated on all pages that need to validate a CheckBox or CheckBoxList.
  3. Create a custom, compiled validation server control that provides the functionality needed. The benefit of this approach is that we have a reusable, easily deployable custom server control that adheres to the validation control metaphor. Unfortunately, this option requires the most upfront code/effort.

Yesterday, I decided to write a custom, compiled validation control for the CheckBox and one for the CheckBoxList using the .NET Framework 2.0 (thereby targetting ASP.NET 2.0). Unlike the CheckBoxListRequiredFieldValidator created by Cenk, these two validators also include client-side script validation support. You can download the CheckBoxValidator and CheckBoxListValidator controls from My Code Projects; a discussion on the CheckBoxList control can be found in the latest 4Guys article, Creating Validator Controls for the CheckBox and CheckBoxList.

posted on Wednesday, September 20, 2006 7:20 AM

Feedback

# re: An ASP.NET 2.0 Validator Web Control for CheckBoxes and CheckBoxLists 9/22/2006 4:52 AM Korayem

Excellent one. I need to create a custom control too for another issue and I am sure yours will be of great guide and aid.

# re: An ASP.NET 2.0 Validator Web Control for CheckBoxes and CheckBoxLists 9/26/2006 8:40 AM Ben Strackany

Good post Scott. I wrote a CheckBoxList validator a few weeks ago & wish I had waited! Also, I didn't realize the issues of FireFox & RegisterExpandoAttribute ... good tip.

# re: An ASP.NET 2.0 Validator Web Control for CheckBoxes and CheckBoxLists 10/20/2006 1:56 PM Joe Wood

Thanks for the article, but I don't understand option #2.

I am trying to use the ASP:CUSTOMVALIDATOR control with a client side javascript and am still getting the "cannot be validated" error.

# re: An ASP.NET 2.0 Validator Web Control for CheckBoxes and CheckBoxLists 1/4/2007 1:35 AM simmy

Is there a way to include the "initialvalue" property like in the regular requiredvalidator, so that this can be used for both checkbox list and dropdownlists?

# re: An ASP.NET 2.0 Validator Web Control for CheckBoxes and CheckBoxLists 1/14/2007 10:06 PM Scot King

Using the CheckBoxValidator and CheckBoxListValidator Controls
To get started using the CheckBoxValidator and CheckBoxListValidator controls in your ASP.NET 2.0 web application, download the code at the end of this article and drop the skmValidators assembly into your application's /Bin folder. You can add the validation controls to Visual Studio 2005's Toolbox - right click on the Toolbox, select Choose Items and browse to skmValidators.dll - or you can manually add the controls using declarative markup and the <%@ Register %gt; directive.

Once you've added the controls to the Toolbox or have entered their markup manually, they should appear as any validation control when in the Designer. Moreover, their properties should be accessible through the Properties window. And most importantly, when visiting a page through a browser, they should function as expected!

Hi,

Where exactly does the .dll file go ? There is NO bin folder as you say. Here is the path to my executable:

"C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\VWDExpress.exe"

Where do I place the skmvalidator.dll so that when I click the "choose items" on the toolbar that it will appear with a checkbox that I can select?

Scot King

# re: An ASP.NET 2.0 Validator Web Control for CheckBoxes and CheckBoxLists 1/15/2007 9:30 AM Scott Mitchell

Scott, put it in the /Bin directory of your Web application. If there is no /Bin directory, create it.

For example, if your web application is rooted at C:\Inetpub\wwwroot\MyWebApp, create a Bin directory and put the DLL in there.

hth

# re: An ASP.NET 2.0 Validator Web Control for CheckBoxes and CheckBoxLists 3/30/2007 2:36 PM Ezequiel Espíndola

Scott, the control doesn't work on a medium trust environment. It throws a permission security error. You need to replace the code accessing the configuration to use GetWebApplicationSection() directly:

XhtmlConformanceSection xhtmlSection = (XhtmlConformanceSection) WebConfigurationManager.GetWebApplicationSection("system.web/xhtmlConformance");

Thanks for the control!

# re: An ASP.NET 2.0 Validator Web Control for CheckBoxes and CheckBoxLists 4/14/2007 4:11 AM Christo Carstens

First, thank you for the article.
I've got one problem with it though and that is that it can't be used when the CheckBoxList is bound to data. Problem1: It throws an exception when itemcount < minimumcheckboxcount. This one is easy to fix and control via a property etc.
Problem2: When databound and the initial item count is 0, I can't get any way for it to fire and do the validation as the table generated by the CheckBoxList control itself does not exist. Can anyone think of ANY way to validate this on the client side by adding something to the control? I know there are many other ways of doing it but I was wondering if it could be possible inside the CheckBoxListValidator control.

Cheers
Christo

# re: An ASP.NET 2.0 Validator Web Control for CheckBoxes and CheckBoxLists 2/1/2008 5:49 AM Vince Stack

To Christo:

I get around the itemcount = 0 issue by setting the MinimumNumberOfSelectedCheckBoxes = 0 declaratively then AFTER databind, I set MinimumNumberOfSelectedCheckBoxes = 1. Works for me.

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<July 2009>
SMTWTFS
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Comment Stats

DayTotal% of Total
Sunday 2046.9%
Monday 42314.3%
Tuesday 50116.9%
Wednesday 54518.4%
Thursday 57219.3%
Friday 53618.1%
Saturday 1856.2%
Total 2966100.0%

Hour1Total% of Total
12:00 AM 752.5%
1:00 AM 802.7%
2:00 AM 672.3%
3:00 AM 812.7%
4:00 AM 642.2%
5:00 AM 1234.1%
6:00 AM 1153.9%
7:00 AM 1755.9%
8:00 AM 1876.3%
9:00 AM 1565.3%
10:00 AM 1866.3%
11:00 AM 1926.5%
12:00 PM 1996.7%
1:00 PM 1846.2%
2:00 PM 1675.6%
3:00 PM 1344.5%
4:00 PM 1153.9%
5:00 PM 1063.6%
6:00 PM 993.3%
7:00 PM 1063.6%
8:00 PM 903.0%
9:00 PM 842.8%
10:00 PM 893.0%
11:00 PM 923.1%
Total 2966100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.91157
Monday 4.92379
Tuesday 4.21471
Wednesday 7.42668
Thursday 6.53666
Friday 5.17450
Saturday 4.73175
Total 5.522966

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 4.0048
8:00 AM 4.29133
9:00 AM 6.04290
10:00 AM 5.83274
11:00 AM 4.36192
12:00 PM 6.44348
1:00 PM 3.14132
2:00 PM 5.04227
3:00 PM 7.97303
4:00 PM 3.8199
5:00 PM 6.00168
6:00 PM 4.56114
7:00 PM 8.95188
8:00 PM 8.58163
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.522966

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles