次の方法で共有


CheckBox Web Server Control Declarative Syntax 

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

<asp:CheckBox
    AccessKey="string"
    AutoPostBack="True|False"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    CausesValidation="True|False"
    Checked="True|False"
    CssClass="string"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnCheckedChanged="CheckedChanged event handler"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    TextAlign="Left|Right"
    ToolTip="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
/>

Remarks

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

NoteNote

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 selected, 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.

NoteNote

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 noteCaution

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 about the CheckBox Web server control's properties and events, see the CheckBox class documentation.

Example

The following example 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 selected.

<asp:CheckBox id="Check1" runat="server"
     Text="CheckBox 1"
     AutoPostBack="True"/>
<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 that contains the billing address of a customer into a text box that contains the shipping address.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CheckBox Web Server Control Declarative Syntax</title>

   <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 id="form1" 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>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CheckBox Example</title>
<script runat="server">

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

   </script>

</head>

<body>

   <form id="form1" 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

Reference

CheckBox

Other Resources

Web Server Control Syntax