Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CheckBoxList

0.00/5 (No votes)
7 Jun 2009 1  
The CheckBoxList control creates a multiselection checkbox group that can be dynamically generated using databinding. To specify items that you want

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

The CheckBoxList control creates a multiselection checkbox group that can be dynamically generated using databinding. 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.  The CheckBoxList control inherits from ListControl, which defines some features such as SelectedValue and SelectedIndex properties.  These properties handle selecting an item in the list for you.  Because the CheckBoxList supports more than one selection, SelectedIndex and SelectedValue return the first selected item's values in the list.

The CheckBoxList control also supports databinding. To bind the control to a datasource, first create a datasource, such as one of the DataSourceControl objects, that contains the items to display in the control. Next, use the 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.

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.

Examples:

Set the TextField and the Value Field from a Datasource (here DataTable) 

DataTable tableCategories = (DataTable) DAL.Category.getCategoryDB();       CheckBoxList1.DataSource = tableCategories;CheckBoxList1.DataValueField = tableCategories.Columns[0].ToString();     CheckBoxList1.DataTextField = tableCategories.Columns[1].ToString();       CheckBoxList1.DataBind(); 

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem></asp:ListItem>      

 * note that this code has its flaws: the schema of the database (Column 0 and 1)  should not be relevant for the presentation (gui, webform)

Links

http://msdn.microsoft.com/en-us/library/8bw4x4wa(VS.71).aspx

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here