YouTip LogoYouTip

Control Htmlinputcheckbox

## ASP.NET HtmlInputCheckBox Control The `HtmlInputCheckBox` control is an HTML server control in ASP.NET used to programmatically control and interact with the HTML `` element on the server side. It allows developers to create, manipulate, and read the state of checkboxes within web forms. --- ## Definition and Usage In standard HTML, the `` element is used to let users select one or more options from a set. By adding the `runat="server"` attribute to this element, ASP.NET converts it into an `HtmlInputCheckBox` control. This enables server-side code to detect whether the checkbox is checked, modify its state dynamically, and respond to user interactions during postbacks. --- ## Properties and Events ### Properties The following table lists the key properties of the `HtmlInputCheckBox` control: | Property | Type | Description | | :--- | :--- | :--- | | `Attributes` | `AttributeCollection` | Returns a collection of all attribute name-and-value pairs declared on the element. | | `Checked` | `Boolean` | Gets or sets a value indicating whether the checkbox is currently selected (checked). `true` if checked; otherwise, `false`. | | `Disabled` | `Boolean` | Gets or sets a value indicating whether the control is disabled. The default is `false`. | | `ID` | `String` | The unique identifier assigned to the control. | | `Name` | `String` | The unique group name for the HTML element. | | `Runat` | `String` | Specifies that the control is a server control. Must be set to `"server"`. | | `Style` | `CssStyleCollection` | Gets a collection of all CSS styles applied to the control. | | `TagName` | `String` | Returns the tag name of the element (always `"input"`). | | `Type` | `String` | Returns the type of the input element (always `"checkbox"`). | | `Value` | `String` | Gets or sets the value associated with the checkbox element. | | `Visible` | `Boolean` | Gets or sets a value indicating whether the control is rendered on the page. | ### Events | Event | Description | | :--- | :--- | | `ServerChange` | Occurs when the state of the checkbox (checked/unchecked) changes between posts to the server. | --- ## Code Example The following example demonstrates how to use the `HtmlInputCheckBox` control in an `.aspx` page. In this example, we declare two `HtmlInputCheckBox` controls, an `HtmlInputButton` control, and an `HtmlGenericControl` (a `

` element) to display the output. When the submit button is clicked, the server-side `SubmitBtn_Click` method runs, checks the state of each checkbox, and updates the paragraph text accordingly. ### ASPX Page (`Default.aspx`) ```html <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> HtmlInputCheckBox Example

Select your favorite colors:

Red
Blue

``` ### Code-Behind (`Default.aspx.cs`) ```csharp using System; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Initialization logic (if any) } protected void SubmitBtn_Click(object sender, EventArgs e) { pMessage.InnerHtml = ""; // Check if the "Red" checkbox is selected if (chkRed.Checked) { pMessage.InnerHtml += "You prefer red!
"; } // Check if the "Blue" checkbox is selected if (chkBlue.Checked) { pMessage.InnerHtml += "You prefer blue!
"; } // If neither is selected if (!chkRed.Checked && !chkBlue.Checked) { pMessage.InnerHtml = "You did not select any color."; } } } ``` --- ## Considerations 1. **Form Requirement**: Like all HTML server controls, the `HtmlInputCheckBox` must be nested inside a `
` tag to function properly and maintain its state across postbacks. 2. **State Persistence**: The `Checked` state of the control is automatically preserved during postbacks using ASP.NET ViewState. 3. **Alternative Control**: ASP.NET also provides a Web Server Control called ``. While `HtmlInputCheckBox` maps directly to a standard HTML `` element, the `` control offers richer built-in features, such as automatic postback (`AutoPostBack="true"`) and built-in label rendering. Use `HtmlInputCheckBox` when you want to keep your HTML markup clean and close to standard HTML5.
← Control HtmlinputfileControl Htmlinputbutton β†’