CheckBox Web Server Control

Creates a check box control that allows the user to switch between a true or false state.

<asp:CheckBox id="CheckBox1" 
     AutoPostBack="True|False"
     Text="Label"
     TextAlign="Right|Left"
     Checked="True|False"
     OnCheckedChanged="OnCheckedChangedMethod"
     runat="server"/>

Remarks

The CheckBox control creates a check box on the Web Forms page that allows the user to switch between a true or false state. You can specify the caption to display in the control by setting the Text property. The caption can appear either on the right or left of the check box. Set the TextAlign property to specify the side that the caption appears on.

Note   Because the <asp:CheckBox> element has no content, you can close the tag with /> instead of using a separate closing tag.

To determine whether the CheckBox control is checked, test the Checked property. The CheckedChanged event is raised when the state of the CheckBox control changes between posts to the server. You can provide an event handler for the CheckedChanged event to perform a specific task when the state of the CheckBox control changes between posts to the server.

Note   When creating multiple CheckBox controls, you can also use the CheckBoxList control. The CheckBoxList control is easier to use for creating a set of check boxes using data binding, while the individual CheckBox control gives you greater control over layout.

By default, the CheckBox control does not automatically post the form to the server when it is clicked. To enable automatic posting, set the AutoPostBack property to true.

CAUTION   Text is not HTML encoded before it is displayed in the CheckBox control. This makes it possible to embed script within HTML tags in the text. If the values for the control come from user input, be sure to validate the values to help prevent security vulnerabilities.

For detailed information on the CheckBox Web server control's properties and events, see the CheckBox Class documentation.

Example

The following shows a sample declaration for a CheckBox control in an .aspx file. The check box is set to immediately post the form back to the server when it is checked.

<asp:CheckBox id=Check1 runat="server"
     Text="CheckBox 1"
     AutoPostBack="True"/>

The following example demonstrates how to create an event handler for the CheckedChanged event to copy the contents of a text box containing the billing address of a customer into a text box that contains the shipping address.

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      Sub Check_Clicked(sender As Object, e As EventArgs) 

         If SameCheckBox.Checked Then
            ShipTextBox.Text = BillTextBox.Text
         Else
            ShipTextBox.Text = ""
         End If

      End Sub

   </script>

</head>

<body>

   <form runat="server">

      <h3>CheckBox Example</h3>








      <table>
         <tr>
            <td>
               Billing Address: <br>
               <asp:TextBox id="BillTextBox"
                    TextMode="MultiLine"
                    Rows="5" 
                    runat="server"/>
            </td>
            <td>
               Shipping Address: <br>
               <asp:TextBox id="ShipTextBox"
                    TextMode="MultiLine"
                    Rows="5" 
                    runat="server"/>
            </td>
         </tr>
         <tr>
            <td>
            </td>
            <td>
               <asp:CheckBox id="SameCheckBox"
                    AutoPostBack="True"
                    Text="Same as billing."
                    TextAlign="Right"
                    OnCheckedChanged="Check_Clicked"
                    runat="server"/>
            </td>
         </tr>
      </table>
            
   </form>
        
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>

   <script runat="server">

      void Check_Clicked(Object sender, EventArgs e) 
      {
         if(SameCheckBox.Checked)
            ShipTextBox.Text = BillTextBox.Text;
         else
            ShipTextBox.Text = "";
      }

   </script>

</head>

<body>

   <form runat="server">

      <h3>CheckBox Example</h3>








      <table>
         <tr>
            <td>
               Billing Address: <br>
               <asp:TextBox id="BillTextBox"
                    TextMode="MultiLine"
                    Rows="5" 
                    runat="server"/>
            </td>
            <td>
               Shipping Address: <br>
               <asp:TextBox id="ShipTextBox"
                    TextMode="MultiLine"
                    Rows="5" 
                    runat="server"/>
            </td>
         </tr>
         <tr>
            <td>
            </td>
            <td>
               <asp:CheckBox id="SameCheckBox"
                    AutoPostBack="True"
                    Text="Same as billing."
                    TextAlign="Right"
                    OnCheckedChanged="Check_Clicked"
                    runat="server"/>
            </td>
         </tr>
      </table>
            
   </form>
        
</body>
</html> 

See Also

Web Server Controls | CheckBox Class