Some sample checkbox form elements:

Default No-Frills HTML Checkbox:
HTML Checkbox With An Associated Value:
Multiple HTML Checkboxs With The Same Name:

ASP.NET CheckBox Control:
ASP.NET CheckBoxList Control:




Let's check out our values just submitted:

FYI: Note that the above form submits back to this same page. If you haven't yet submit the form then there won't be any values in the text below since they are retrieved from the form. In this case, the values reflected are simply the defaults and are the same results you would get if you submitted the form without checking anything.

Just because we're using ASP.NET doesn't mean we have to use ASP.NET server controls. The first couple samples use plain old HTML and work just fine. In fact, unless you need the functionality offered by the CheckBox or CheckBoxList controls using plain old HTML actually uses fewer server resources!

Default No-Frills HTML Checkbox

By default, an HTML checkbox element returns nothing if it wasn't checked and a value of "on" if it was. So by checking it's value we can determine if it was checked or not when the form was submitted. The No-Frills Checkbox was: Not Checked.

HTML Checkbox With An Associated Value

If you don't like the fact that the checkbox returns the value of "on" it's really easy to change. Simply add a value parameter and assign to it what you'd like the value returned by the checkbox to be. The Checkbox With An Associated Value returned: .

Multiple HTML Checkboxs With The Same Name

Probably one of the most useful uses of the checkbox is to allow users to check multiple selections. Instead of having to chech each box's status individually you can simply name them all the same and the results will be returned in a convenient collection like fashion.

You can access them in a couple of ways. First you can use the "For Each" syntax illustrated here (in the source code at least): .

Or even easier is to just get them as a comma delimited list: .

ASP.NET CheckBox Control

This is just a plain old ASP.NET CheckBox control, but just by it being an ASP.NET control we get all the associated benefits. For example, we can programmatically manipulate it (like I do by adding the label to it) and it keeps its state across page reloads.

ASP.NET CheckBox Control was: Not Checked.

ASP.NET CheckBoxList Control

This is the ASP.NET control for dealing with a group of checkboxes. You can do all sorts of cool stuff with it. I'm going to keep things pretty simple, but I do play with the RepeatLayout and RepeatDirection and use databinding to fill the list with the individual checkboxes.

ASP.NET CheckBoxList Control contained the following selected items: .


Click here to read about and download the source code.