<asp:CheckBox> Control

The <asp:CheckBox> control creates a single checkbox that can be used to input a user choice to a script. Multiple CheckBoxes can be configured for additional choices. Unlike multiple RadioButton controls, multiple CheckBoxes do not provide mutually exclusive choices. All or none of a set of CheckBox controls can be checked. An example set of CheckBoxes is shown below. Clicking the "Select" button reports the choices made.

Choose one or more checkboxes:







Figure 6-28. Using CheckBox controls.

Format for CheckBox Control

The general format for the <asp:CheckBox> control is shown below. As many CheckBox controls are created as are needed for display.

<asp:CheckBox id="id" Runat="Server"
  AutoPostBack="True|False"
  Checked="True|False"
  Text="string"
  TextAlign="Left|Right"

    property="value"...

    OnCheckedChanged="subprogram" 
/>
Figure 6-29. General format for <asp:CheckBox> control.

The Text attribute provides a label for the CheckBox. With the TextAlign attribute the label can appear to the right or left of the CheckBox. A CheckBox can be prechecked by coding Checked="True". Since no value is associated with a CheckBox, the script determines if it is checked by testing its Checked property. However, the Text property of the control can be substituted for its value.

Normally, CheckBoxes are tested by a script called by an associated button. Alternately, the CheckBoxes themselves can trigger a script by setting their AutoPostBack="True" property and identifying a subprogram to run in the OnCheckedChanged property.

Scripting CheckBoxes

CheckBoxes can be presented as a group of choices; however, these are not mutually exclusive choices as in the case of radio buttons that are grouped under a GroupName. Each CheckBox is a separate and distinct control that must be tested individually by a script to determine its Checked property. The following script and code works in this fashion to produce the display at the top of this page.

<SCRIPT Runat="Server">

Sub Get_CheckBoxes (Src As Object, Args As EventArgs)

  If CheckBox1.Checked Then
    Message.Text &= "CheckBox 1 was checked<br/>"
  End If
  If CheckBox2.Checked Then
    Message.Text &= "CheckBox 2 was checked<br/>"
  End If
  If CheckBox3.Checked Then
    Message.Text &= "CheckBox 3 was checked<br/>"
  End If

  If Not CheckBox1.Checked AND Not CheckBox2.Checked _
  AND Not CheckBox3.Checked Then
    Message.Text = "Please select a CheckBox"
  End If

End Sub

</SCRIPT>

<form Runat="Server">

<b>Choose one or more checkboxes:</b><br/>
<asp:CheckBox id="CheckBox1" Text="CheckBox 1" Runat="Server"/><br/>
<asp:CheckBox id="CheckBox2" Text="CheckBox 2" Runat="Server"/><br/>
<asp:CheckBox id="CheckBox3" Text="CheckBox 3" Runat="Server"/><br/><br/>
<asp:Button Text="Select" OnClick="Get_CheckBoxes" Runat="Server"/><br/>
<asp:Label id="Message" EnableViewState="False" Runat="Server"/>

</form>
Listing 6-23. Code and script to create and process CheckBoxes.

Notice that the condition tests are separate If...End If statements in order to test each CheckBox's Checked property individually. Label output is a list of one or more concatenated lines. Therefore, the Label must have its View State turned off so that repeated button clicks do not append lines to those already displayed by a previous button click.

The following example modifies the previous rectangle-creation script to permit a choice of color for the background of the <asp:Panel> control. Unlike the case of using mutually exclusive radio buttons where the choice is one of three colors, using checkboxes permits a combination of colors since more than one can be chosen.

Make a Rectangle

Width Height Color Area
 x 


  

Figure 6-30. Producing a colored rectangle from CheckBox selections.
<%@ Import Namespace="System.Drawing" %>

<SCRIPT Runat="Server">

Sub Make_Rectangle (Src As Object, Args As EventArgs)
	
  If IsNumeric(Width.Text) AND IsNumeric(Height.Text) Then
    
    Area.Text = FormatNumber(Width.Text * Height.Text, 0)
    
    Rectangle.Width = Unit.Parse(Width.Text)
    Rectangle.Height = Unit.Parse(Height.Text)
    Rectangle.BorderStyle = BorderStyle.Solid
    Rectangle.BorderWidth = Unit.Parse("1px")
		
    Dim HexRed, HexGreen, HexBlue As String
    HexRed = "00"
    HexGreen = "00"
    HexBlue = "00"
    If Red.Checked = True Then
      HexRed = "FF"
    End If
    If Green.Checked = True Then
      HexGreen = "FF"
    End If
    If Blue.Checked = True Then
      HexBlue = "FF"
    End If
    Rectangle.BackColor = Color.FromName("#" & HexRed & HexGreen & HexBlue)

  End If
	
End Sub

</SCRIPT>

<form Runat="Server">

<h3>Make a Rectangle</h3>

<table border="0" cellpadding="0">
<tr>
  <th>Width</th>
  <th>Height</th>
  <th>Color</th>
  <th></th>
</tr>
<tr valign="top">
  <td><asp:TextBox id="Width" Runat="Server"
        Columns="1"
        MaxLength="3"
        Text="100"
        Style="text-align:right"/>
  </td>
  <td><asp:TextBox id="Height" Runat="Server"
        Columns="1"
        MaxLength="3"
        Text="50"
        Style="text-align:right"/>
  </td>
  <td><asp:CheckBox id="Red" Text="Red" Runat="Server"/><br>
      <asp:CheckBox id="Green" Text="Green" Runat="Server"/><br>
      <asp:CheckBox id="Blue" Text="Blue" Runat="Server"/><br>
  </td>
  <td><asp:Button Runat="Server" 
        Text="Make"
        OnClick="Make_Rectangle"/>
  </td>
</tr>
</table>

<asp:Panel id="Rectangle" Runat="Server"/>
Listing 6-24. Code and script to produce a colored rectangle from CheckBox selections.

Recall that one of the ways of specifying colors is by a hexadecimal value. The color is a sequence of two-digit hexadecimal values for red, green, and blue, with FF being the highest saturation and 00 the lowest saturation of the color. For example, the hexadecimal value #FF0000 produces pure red by specifying the highest saturation for red (FF) and the lowest saturation for green (00) and blue (00).

It is easy, then, to construct a hexadecimal string representing a color value by concatenating the saturation values for red, green, and blue. In the script, a checked box is taken to mean selection of the highest value (FF) for a color; an unchecked box means the lowest value (00). Initially, these hexadecimal values are set to the lowest value in three variables representing red, green, and blue:

  HexRed = "00"
  HexGreen = "00"
  HexBlue = "00"

If the color's checkbox is checked, the value is reset to FF. Then the values are concatenated into a single string ("#" & HexRed & HexGreen & HexBlue) to produce a color. Of course, the color selections are quite limited, but the idea here is to determine the checked status of checkboxes, not to produce a rainbow of colors.

Post-Back of Checked Boxes

It is seldom necessary to activate a script with the click of a single checkbox in a group, so it is seldom necessary to code checkboxes with AutoPostBack and OnCheckedChanged properties. The reason is that, normally, a group of checkboxes is presented from which one or more choices can be made from the list. Therefore, a subprogram should not be called until after all the boxes are checked. To trigger the subprogram call when any one checkbox is clicked does not provide the subprogram with complete information about choices.

You can, of course, trigger a script with display and post-back of a single checkbox. In this case, though, there is no advantage to using a checkbox over a standard button.