Click here to Skip to main content
15,894,405 members
Articles / Properties
Article

CheckBoxList

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 15.6K   1   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

 

This article was originally posted at http://wiki.asp.net/page.aspx/323/checkboxlist

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
GeneralNice one Pin
Member 1338498231-Aug-17 1:20
Member 1338498231-Aug-17 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.