Udostępnij za pośrednictwem


HtmlInputCheckBox Server Control Declarative Syntax

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"
    EnableViewState="False|True"
    Id="string"
    Visible="False|True"
    OnDataBinding="OnDataBinding event handler"
    OnDisposed="OnDisposed event handler"
    OnInit="OnInit event handler"
    OnLoad="OnLoad event handler"
    OnPreRender="OnPreRender event handler"
    OnServerChange="OnServerChange event handler"
    OnUnload="OnUnload event handler"
    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" %>
<!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>
<title>HtmlInputCheckBox Control</title>

   <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 id="Form1" runat="server">
        <input id="Check1" type="checkbox" runat="server" checked="checked"/> 
            CheckBox1 &nbsp;&nbsp;
        <span id="Span1" style="color:red" runat="server" />
        <br />
        <input type="button" id="Button1" value="Enter"
               onserverclick="Button1_Click" runat="server"/>
    </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>
<title>HtmlInputCheckBox Control</title>

    <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 id="Form1" runat="server">
        <input id="Check1" type="checkbox" runat="server" checked="checked"/> 
            CheckBox1 &nbsp;&nbsp;
        <span id="Span1" style="color:red" runat="server" />
        <br />
        <input type="button" id="Button1" value="Enter"
               onserverclick="Button1_Click" runat="server"/>
    </form>
</body>
</html>

See Also

Reference

HtmlInputCheckBox

Other Resources

HTML Server Controls