You can use two types of Web server controls to add check boxes to a Web Forms page: individual
![]() |
---|
You can also use the HtmlInputCheckBox control to add check boxes to a Web Forms page. For more information, see |
CheckBox Control versus CheckBoxList Control
You add individual CheckBox controls to a page and work with them singly. Alternatively, the CheckBoxList control is a single control that acts as a parent control for a collection of check-box list items. It derives from a base
Each type of control has advantages. By using individual CheckBox controls, you get more control over the layout of the check boxes on the page than by using the CheckBoxList control. For example, you can include text (that is, non-check-box text) between each check box. You can also control the font and color of individual check boxes.
The CheckBoxList control is a better choice if you want to create a series of check boxes from data in a database. (You can still bind an individual CheckBox control to data.)
CheckBox Events
Events work differently between individual CheckBox controls and the CheckBoxList control.
Individual CheckBox Controls
Individual CheckBox controls raise the
![]() |
---|
The ability of a CheckBox control to post to the server when it is checked requires that the browser support ECMAScript (JavaScript) and that scripting be enabled on the user's browser. |
Whether a CheckBox control posts to the server or not, you might not need to create an event handler for the CheckedChanged event. You can test which check box is selected in the handler. Typically, you create an event handler for the CheckedChanged event only if you need to know that the check box was changed, not just to read the value of a check box.
CheckBox Control HTML Attributes
When the CheckBox control renders to the browser, it does so in two parts: an input element representing the check box, and a separate label element representing the caption for the check box. The combination of the two elements is in turn wrapped in a span element.
When you apply style or attribute settings to a CheckBox control, the settings are applied to the outer span element. For example, if you set the control's
At times, you might want to be able to apply separate settings to the check box and to the label. The CheckBox control supports two properties that you can set at run time: the
[Visual Basic]
В | ![]() |
---|---|
CheckBox1.InputAttributes.Add("onmouseover", _ "this.style.backgroundColor = 'red'") CheckBox1.InputAttributes.Add("onmouseout", _ "this.style.backgroundColor = 'white'") CheckBox1.InputAttributes.Add("onmouseover", "this.style.backgroundColor = 'red'"); CheckBox1.InputAttributes.Add("onmouseout", "this.style.backgroundColor = 'white'"); |
CheckBoxList Control
In contrast, the CheckBoxList control raises a
As with individual CheckBox controls, it is more common to test the state of the CheckBoxList control after the form has been posted some other way.
Binding Data to the Control
As with any Web server control, you can bind an individual CheckBox control to a data source, and you can bind any property of the CheckBox control to any field of the data source. For example, it is typical to set the control's
You can also bind a CheckBoxList control to a data source. In that case, the check boxes each represent a different record in the data source.
See Also
Tasks
How to: Add CheckBox Web Server Controls to a Web Forms PageHow to: Add CheckBoxList Web Server Controls to a Web Forms Page
How to: Add Items in List Web Server Controls
How to: Determine the Selection in List Web Server Controls
How to: Populate List Web Server Controls from a Data Source
How to: Respond to Changes in List Web Server Controls
How to: Set the Selection in List Web Server Controls
