Checking CheckBox and CheckBoxList Controls
Creates a multiselection check box group. This control supports binding to a data source.
<asp:CheckBoxList id="CheckBoxList1" AutoPostBack="True|False" CellPadding="Pixels" DataSource='<% databindingexpression %>' DataTextField="DataSourceField" DataValueField="DataSourceField" RepeatColumns="ColumnCount" RepeatDirection="Vertical|Horizontal" RepeatLayout="Flow|Table" TextAlign="Right|Left" OnSelectedIndexChanged="OnSelectedIndexChangedMethod" runat="server"> <asp:ListItem value="value" selected="True|False"> Text </asp:ListItem> </asp:CheckBoxList>
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. Array List object, that contains the items to display in the control. Next, use the Control .Data Bind 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 Repeat Layout and Repeat Direction properties. If RepeatLayout is set toRepeatLayout.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 toRepeatDirection.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 Check Box List 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.