Home > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlInputCheckBox
ASP.NET Syntax ASP.NET Syntax for HTML Controls
Creates a selectable check box control.
<input type = checkbox id="accessID" runat="server" checked >
For information on the individual members of this class, see HtmlInputCheckBox in the class library.
- This control does not require a closing tag.
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.
In this sample, when a user clicks the input button included 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 <span
> control that depends on the value of the property.
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.
- In the <
body
> of the Web Forms page, declare an HtmlForm control to contain three HtmlInputCheckBox controls, the HtmlInputButton, and a <span 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 id="Button1" value="Show me"
onServerClick = "Button1_Click" runat="server">
<p><span id="Message" runat="server" />
</form>
- In the <
head
> of the Web Forms page, define the handler for the button’s click event.
<script language="C#" runat="server">
void Button1_Click ( object Source, EventArgs e ) {
if ( Check1.Checked || Check2.Checked || Check3.Checked ) {
Message.InnerHtml = "Looks like you prefer: <ul>";
if ( Check1.Checked ) Message.InnerHtml += "<li>"+Check1.Value;
if ( Check2.Checked ) Message.InnerHtml += "<li>"+Check2.Value;
if ( Check3.Checked ) Message.InnerHtml += "<li>"+Check3.Value;
Message.InnerHtml += "</ul>";
}
else Message.InnerHtml = "Looks like you don’t prefer anything.";
}
</script>
- 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 >
Web Forms Events and Handlers HtmlInputCheckBox Class