ASP 101 - Active Server Pages 101 checkbox.aspx
<%@ Page Language="VB" %>
<script language="VB" runat="server">
	Sub Page_Load(sender As Object, e As EventArgs)
		' Retrieve and display the values for each checkbox/group
		' Default No-Frills HTML Checkbox:
		' Note that "on" is in lower case.  Since writing "on"
		' doesn't do much good, I simply check for it to determine
		' which branch of the conditional to execute.  I could
		' place whatever code I wanted inside either condition.
		' In this case I simply print out a "user-friendly" message.
		If Request.Form("default") = "on" Then
			lblHtmlNoFrills.Text = "Checked"
		Else
			lblHtmlNoFrills.Text = "Not Checked"
		End If
		'Checkbox With An Associated Value:
		lblHtmlValue.Text = Request.Form("value_specified")
		'Multiple HTML Checkboxs:
		lblHtmlMultiForEach.Text = ""
		If Not(Request.Form("multiple") Is Nothing) Then
			Dim Item
			For Each Item In Request.Form("multiple")
				' Do Whatever you want to do with each one.
				' I'll just write them out with spaces after
				' each one so they don't run together.
				lblHtmlMultiForEach.Text = lblHtmlMultiForEach.Text & Item & " "
			Next
		End If
		lblHtmlMultiList.Text = Request.Form("multiple")
		' ASP.NET CheckBox Control:
		chkAspNetCheckBox.Text = " ASP.NET CheckBox Control Text Label"
		If chkAspNetCheckBox.Checked Then
			lblAspNetCheckBox.Text = "Checked"
		Else
			lblAspNetCheckBox.Text = "Not Checked"
		End If
		' ASP.NET CheckBoxList Control:
		' On the first run we fill our CheckBoxList with something.
		' You can also fill it manually, but that's no fun!  ;)
		If Not Page.IsPostBack Then
			Dim arrCBLItems(2) As String
			arrCBLItems(0) = "Item 1"
			arrCBLItems(1) = "Item 2"
			arrCBLItems(2) = "Item 3"
			cblAspNetCheckBoxList.DataSource = arrCBLItems
			cblAspNetCheckBoxList.DataBind
		End If
		Dim I
		lblAspNetCheckBoxList.Text = ""
		For I = 0 To cblAspNetCheckBoxList.Items.Count - 1
			If cblAspNetCheckBoxList.Items(I).Selected Then
				lblAspNetCheckBoxList.Text = lblAspNetCheckBoxList.Text _
					& cblAspNetCheckBoxList.Items(I).Text & " "
			End If
		Next
	End Sub
</script>
<html>
<head>
<title>ASP.NET Checkbox Sample</title>
</head>
<body>
<form runat="server">
  <h2>Some sample checkbox form elements:</h2>
  Default No-Frills HTML Checkbox:
  <input type="checkbox" name="default" />
  <br />
  HTML Checkbox With An Associated Value:
  <input type="checkbox" name="value_specified" value="Some Value" />
  <br />
  Multiple HTML Checkboxs With The Same Name:
  <input type="checkbox" name="multiple" value="1" />
  <input type="checkbox" name="multiple" value="2" />
  <input type="checkbox" name="multiple" value="3" />
  <br />
  <br />
  ASP.NET CheckBox Control:
  <asp:CheckBox id="chkAspNetCheckBox" runat="server" />
  <br />
  ASP.NET CheckBoxList Control:
  <asp:CheckBoxList id="cblAspNetCheckBoxList" runat="server"
    RepeatLayout    = "Flow"
    RepeatDirection = "Horizontal"
  />
  <br />
  <br />
  <input type="submit" />
</form>
<br />
<br />
<br />
<h2>Let's check out our values just submitted:</h2>
<blockquote><em>
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.
</em></blockquote>
<p>
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!
</p>
<p><strong>Default No-Frills HTML Checkbox</strong></p>
<p>
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:
<strong><asp:Label id="lblHtmlNoFrills" runat="server" /></strong>.
</p>
<p><strong>HTML Checkbox With An Associated Value</strong></p>
<p>
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:
<strong><asp:Label id="lblHtmlValue" runat="server" /></strong>.
</p>
<p><strong>Multiple HTML Checkboxs With The Same Name</strong></p>
<p>
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.
</p>
<p>
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):
<strong><asp:Label id="lblHtmlMultiForEach" runat="server" /></strong>.
</p>
<p>
Or even easier is to just get them as a comma delimited list:
<strong><asp:Label id="lblHtmlMultiList" runat="server" /></strong>.
</p>
<p><strong>ASP.NET CheckBox Control</strong></p>
<p>
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.
</p>
<p>
ASP.NET CheckBox Control was:
<strong><asp:Label id="lblAspNetCheckBox" runat="server" /></strong>.
</p>
<p><strong>ASP.NET CheckBoxList Control</strong></p>
<p>
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.
</p>
<p>
ASP.NET CheckBoxList Control contained the following selected items:
<strong><asp:Label id="lblAspNetCheckBoxList" runat="server" /></strong>.
</p>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/checkbox_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>