Checking CheckBox and CheckBoxList Controls

Learning Resources

Checking CheckBox and CheckBoxList Controls

Creates a multiselection check box group. This control supports binding to a data source.



The CheckBoxList control creates a multiselection check box group that can be dynamically generated using data binding. To specify items that you want to appear in the CheckBoxList control, place a ListItem element for each entry between the opening and closing tags of the CheckBoxList control.

Note   You can also use multiple CheckBox controls. The CheckBoxList control is easier for creating a set of check boxes using data binding, while the individual CheckBox control gives you greater control over layout.

The CheckBoxList control also supports data binding. To bind the control to a data source, first create a data source, such as a System.Collections.ArrayList object, that contains the items to display in the control. Next, use the Control.DataBind method to bind the data source to the CheckBoxList control. Use the DataTextField and DataValueField properties to specify which field in the data source to bind to the Text and Value properties of each list item in the control, respectively. The CheckBoxList control will now display the information from the data source.

To determine the selected items in the CheckBoxList control, iterate through the Items collection and test the Selected property of each item in the collection.

You can specify the way the list is displayed by using the RepeatLayout and RepeatDirection properties. If RepeatLayout is set to RepeatLayout.Table (the default setting), the list is rendered within a table. If it is set to RepeatLayout.Flow, the list is rendered without any table structure. By default, RepeatDirection is set to RepeatDirection.Vertical. Setting this property to RepeatDirection.Horizontal renders the list horizontally.

CAUTION   Text is not HTML encoded before it is displayed in the CheckBoxList control. This makes it possible to embed script within HTML tags in the text. If the values for the control come from user input, be sure to validate the values to help prevent security vulnerabilities.

For detailed information on the CheckBoxList Web server control's properties and events, see the CheckBoxList Class documentation.


The following shows a sample declaration for a CheckBoxList control in an .aspx file. The list contains six items that are not mutually exclusive. When a user checks one of the boxes, the page is not immediately posted back to the server (the page is not posted until some other event occurs, such as a Button click). No OnSelectedIndexChanged event handler is called because no method is declared for that event.

   Item 1
   Item 2
   Item 3
   Item 4
   Item 5
   Item 6



 For Support