asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlInputCheckBox

HtmlInputCheckBox Control Syntax

HtmlInputCheckBox Class   Input Checkbox Element


Creates a selectable check box control.

Declarative Syntax

For information on the individual members of this class, see HtmlInputCheckBox in the class library.

Syntax Notes

  • This control does not require a closing tag.

Working with HtmlInputCheckBox

The HtmlInputCheckBox control enables programming of the HTML <input type=checkbox> element.

The control accepts boolean ( true/false ) input. When checked, its Checked property returns true.

HtmlInputCheckBox is typically used with other user input controls, such as the HtmlInputButton control, to determine whether a check box is selected. This is done by evaluating the Checked property on this control.

This sample illustrates use of the HtmlInputCheckBox control.

Getting and Setting the Checked Value of an HtmlInputCheckBox

In this sample, when a user clicks the input button on the form, the button’s click event handler evaluates the value of each HtmlInputCheckBox control’s Checked property, then displays a message in a <p> control that depends on the value of the property.

Setting and Getting the Checked Value of an HtmlInputCheckBox
Run Sample | View Source

Note that the HtmlCheckBox control does not have an event that automatically notifies the server when a user has selected the control.

You can use the checkbox with one of the button controls — such as the HtmlInputButton, HtmlInputImage, or HtmlButton — to cause a postback that will allow to program the HtmlCheckBox control itself.

  1. In the <body> of the Web Forms page, declare an HtmlForm control to contain three HtmlInputCheckBox controls, the HtmlInputButton, and a <p runat="server"> control.
    <form runat="server">
    
       <input type=checkbox id="Check1" value="Strawberry" runat="server" />Strawberry<br>
       <input type=checkbox id="Check2" value="Vanilla" runat="server" />Vanilla<br>
       <input type=checkbox id="Check3" value="Chocolate" runat="server" />Chocolate
    
       <p><input type=button value="Show me" onServerClick="getChecked" runat="server">
    
       <p id="msg" runat="server" />
    
    </form>
  2. In the <head> of the Web Forms page, define the handler for the button’s click event.
    <script language="C#" runat="server">
    void getChecked ( object Source, EventArgs e ) {
       if ( Check1.Checked || Check2.Checked || Check3.Checked ) {
          msg.InnerHtml = "Looks like you prefer: ";
          if ( Check1.Checked ) msg.InnerHtml += " " + Check1.Value;
          if ( Check2.Checked ) msg.InnerHtml += " " + Check2.Value;
          if ( Check3.Checked ) msg.InnerHtml += " " + Check3.Value;
       }
       else msg.InnerHtml = "Looks like you're not a yogurt fan.";
    }
    </script>
  3. If you need to initially set the checkbox’s Checked value to true, include the Checked keyword in the HtmlInputCheckBox declaration:
<input type=checkbox id="Check1" runat="server" checked>

While this method may work for a few checkboxes, it may be crude and impractrical in situations where there may be more checkboxes to evaluate. The usual solution is to loop thru all the checkbox controls on the page and call a function each time a checked value is encountered.

More about that in a later chapter.

See Also

HtmlInputCheckBox Class   Input Checkbox Element   Web Forms Events and Handlers



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note