asp.net.ph

Skip Navigation Links

Introduction to the CheckBox and CheckBoxList Controls

Controls You Can Use on Web Forms   ASP.NET Standard Controls   CheckBox and CheckBoxList Controls


You can use two types of Web server controls to add check boxes to a Web Forms page: individual CheckBox controls or a CheckBoxList control. Both controls provide a way for users to input Boolean data: true or false, yes or no.

NOTE: You can also use the HtmlInputCheckBox server control to add check boxes to a Web Forms page. For details about the differences between ASP.NET and HTML server controls, see Introduction to Web Forms Server Controls.

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 ListControl Class class, and therefore works much like the ListBox Control, DropDownList Control, and RadioButtonList Control Web server controls. Therefore, many of the procedures for working with the CheckBoxList control are the same as they are for the other list Web server controls.

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. 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 check boxes individually.

The CheckBoxList control is a better choice if you want to create a series of check boxes out of data in a database. ( You can still bind an individual CheckBox control to data ) .

CheckBox Events

Events work slightly differently between individual CheckBox controls and the CheckBoxList control.

Individual CheckBox Controls

Individual CheckBox controls raise an event — CheckedChanged — when users click the control. By default, this event does not cause the page to be posted to the server, but you can have the control force an immediate post by setting the AutoPostBack property to true.

Whether a CheckBox control posts to the server or not, you might not need to create an event-handling method for the CheckedChanged event. You can test which check box is selected when the form has been posted to the server by a control such as a Button control. Typically, you create an event-handling method for the CheckedChanged event only if you need to know that the check box was changed.

CheckBoxList Control

In contrast, the CheckBoxList control raises a SelectedIndexChanged event when users select any check box in the list. By default, the event does not cause the form to be posted to the server, although you can specify this option by setting the AutoPostBack property to true. For details, see Responding to Changes in a List Control.

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. For details, see Determining the Selection in a List Control.

Binding Data to the Control

As with any Web server control, you can bind an individual CheckBox control to a data source, and 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 Checked property from information in a database.

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. For details about binding the CheckBoxList control to data, see Populating a List Control from a Database.

See Also

Adding Web Server Controls to a Web Forms Page   Introduction to Validating User Input in Web Forms   Setting Web Forms Server Control Properties   Web Forms Server Controls and CSS Styles   Web Forms Events and Handlers   Introduction to Web Forms State Management



© 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