다음을 통해 공유


HtmlInputCheckBox Control

Creates a server-side control that maps to the <input type=checkbox> HTML element and allows you to a create check box control that lets the user select a true or false state.

<input type=checkbox
       id="programmaticID"
       checked
       runat="server" >

Remarks

Use the HtmlInputCheckBox control to program against the <input type=checkbox> HTML element. The HtmlInputCheckBox control does not post back to the server when it is clicked. The state of the check box is sent to the server for processing when you use a control that posts back the server, such as the HtmlInputButton control. To determine whether the check box is selected, test the Checked property of the control.

Note   This control does not require a closing tag.

Example

The following example demonstrates how to create an HtmlInputCheckBox control that allows the user to select a true or false state. When a user clicks the input button included on the page, the Button1_Click event handler determines whether the HtmlInputCheckBox control is checked. It then displays a message in a <span> control. Note that even though the checked value is set to true by default in this example, the user still needs to click Button1 to display the text.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub Button1_Click(Source As Object, e As EventArgs)
         If Check1.Checked = True Then
            Span1.InnerHtml = "Check1 is checked!"
         Else
            Span1.InnerHtml = "Check1 is not checked!"
         End If
      End Sub
   </script>
</head>
<body>

    <h3>HtmlInputCheckBox Sample</h3>

    <form runat="server">
        <input id="Check1" type=checkbox runat="server" checked/> 
            CheckBox1 &nbsp;&nbsp;
        <span id=Span1 style="color:red" runat="server" />
        <p>
        <input type=button id="Button1" value="Enter"
               OnServerClick="Button1_Click" runat="server"/>
    </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
    <script runat="server">
       void Button1_Click(object Source, EventArgs e) 
       {
          if (Check1.Checked == true) 
          {
             Span1.InnerHtml = "Check1 is checked!";
          }
          else 
          {
             Span1.InnerHtml = "Check1 is not checked!";
          }
       }
    </script>
</head>
<body>

    <h3>HtmlInputCheckBox Sample</h3>

    <form runat="server">
        <input id="Check1" type=checkbox runat="server" checked/> 
            CheckBox1 &nbsp;&nbsp;
        <span id=Span1 style="color:red" runat="server" />
        <p>
        <input type=button id="Button1" value="Enter"
               OnServerClick="Button1_Click" runat="server"/>
    </form>
</body>
</html>

See Also

ASP.NET Syntax for HTML Controls | HtmlInputCheckBox Class