The CheckBox Web control enables users to set true-or-false values in the page.
Note You can also use the CheckBoxList control. The CheckBoxList control is easier for data binding, while the individual CheckBox control gives you greater control over the layout of the controls.
Required properties are noted in boldface type.
<asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True|False" Text="label" TextAlign="Right|Left" Checked="True|False" OnCheckedChanged="OnCheckedChangedMethod" />
Note Because the <asp:CheckBox> element has no content, you can close it with />.
Property | Description |
---|---|
(Base control properties) | The properties defined in Base Web Control Properties. |
AutoPostBack | True if client-side changes in the check box automatically cause a postback to the server; false otherwise. The default is false. |
Checked | True if the check box is checked, false otherwise. The default is false. |
TextAlign | The position of the caption. Possible values are Right and Left The default is Right.
When programming, you set this property using the TextAlign enumeration. |
Text | The check box caption. |
Event (and paramters) | Description |
---|---|
OnCheckedChanged(Object sender, EventArgs e) | Raised when the user clicks the checkbox. This event does not cause the Web Forms page to be posted to the server unless the AutoPostBack property is set to true.
The e events argument object has no properties. |
The following shows a sample declaration for a CheckBox control in an .aspx file. The check box is set to immediately post the form when checked.
<asp:CheckBox id=Check1 runat="server" Text="CheckBox 1" AutoPostBack="True" />
The following example shows an event-hanlding method for the CheckBox control. Because the control posted the page immediately to the server, the event is handled with the Page object's Page_Load event-handling method.
[C#]
protected void Page_Load(Object Sender, EventArgs e)
{
Label1.Text = Check1.Checked ? "Check1 is checked" :
"Check1 is not checked";
}
See Also