Introduction
Working with the CheckBoxList Control
The CheckBoxList control consists of a group of check boxes that actually provides a multi-selection checkbox with the capability of selecting one or more items from the list of items. Compared to the CheckBox control, a CheckBoxList control is a preferred choice in cases where you might require creation of a series of check boxes and populate them with data from a data store like, a database table, an Xml file or even a web service that fetches data.
Main
Appending List Items to the CheckBoxList Control
The following figure shows how a CheckBoxList control looks like at runtime when it is bound with data.

This section discusses how you can accomplish the above, that is, add list items to the CheckBoxList control. You can create a CheckBoxList control and add static data to it using list items as shown in the following code snippet:
<asp:checkboxlist id=dept runat="server">
<asp:listitem id=1 runat="server" value="IT"></asp:listitem>
<asp:listitem id=2 runat="server" value="Sales"></asp:listitem>
<asp:listitem id=3 runat="server" value="Admin"></asp:listitem>
<asp:listitem id=4 runat="server" value="HR"></asp:listitem>
</asp:checkboxlist>
You can also add items to the CheckBoxList control programmatically. Refer to the following code snippet:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CheckBoxList1.AutoPostBack = true;
CheckBoxList1.RepeatColumns = 1;
CheckBoxList1.RepeatDirection = RepeatDirection.Vertical;
CheckBoxList1.RepeatLayout = RepeatLayout.Flow;
CheckBoxList1.TextAlign = TextAlign.Right;
CheckBoxList1.Items.Add(new ListItem("James"));
CheckBoxList1.Items.Add(new ListItem("Johny"));
CheckBoxList1.Items.Add(new ListItem("Andy"));
CheckBoxList1.Items.Add(new ListItem("Mike"));
}
}
Selecting One or More List Items
You can find out which item in this list has been selected by iterating through the items collection of this control. The following code snippet illustrates how you can retrieve the list items that have been selected from CheckBoxList control called dept by iterating through and checking the Selected property of each list item.
string message = String.Empty;
for(int i=0; i < dept.Items.Count; i++)
{
if(dept.Items[i].Selected)
message += dept.Items[0].Text;
}
lblDept.Text = message;
Note that the CheckBoxList class does not contain a SelectedItems property. How about implementing a Custom CheckBoxList control that contains a SelectedItems property that can be used to select one or more list items from the control? We will design and implement a Custom CheckBoxList control to accomplish this later below.
Removing List Items from the CheckBoxList Control
To remove a specific list item from the list item collection of the CheckBoxList control, use the RemoveAt() method of the Items collection property of the control as shown in the following code snippet:
CheckBoxList1.Items.RemoveAt(0);
To remove all the list items from the CheckBoxList control, use the following code:
CheckBoxList1.Items.Clear();
Here, CheckBoxList1 is the name of the CheckBoxListcontrol.
Binding Data to the CheckBoxList Control
Like the ListBox control, you can bind data to the CheckBoxList control in 2 ways:
Declarative and Programmatic.
We have already discussed how we can bind data to this control declaratively. For programmatic data binding to the CheckBoxListcontrol you have to use a valid data source and the DataBind() method.
As usual, you need to specify the DataTextField and the DataValueField properties to specify the Textand Valueproperties of each of the list items in the control. You can also set the Checked property of the all the CheckBoxcontrols in the list to either true or false programmatically.
When a Checked property of any of the CheckBoxcontrols in this list is set to true, the control is checked, that is, a check-mark appears in the control. When the same property is false, the control is unchecked.
We have already discussed how we can bind static data to the CheckBoxList control declaratively through the .aspx page.
The following code snippet illustrates how you can bind data to this with data from an external data source programmatically.
protected void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
DataManager dataManager = new DataManager();
checkBoxList.DataSource = dataManager.GetDataFromArrayList();
checkBoxList.DataTextField="EmpName";
checkBoxList.DataValueField="EmpCode";
checkBoxList.DataBind();
}
}
Handling CheckBoxList Control Events
The SelectedIndexChanged event of the CheckBoxList control is fired whenever you select any check box in the list, that is, the SelectedIndex of the control changes. The following code snippet shows how this event can be used for this control:
private void checkBoxList_SelectedIndexChanged(object sender,System.EventArgs e)
{
//Custom code to handle this event
}
In the section that follows, we will take a look at how we can handle events with a CustomCheckBoxList control that we will implement by extending the CheckBoxList control.
Conclusion
The ASP.NET CheckBoxList control is used to create a defined list of options in which a user can choose from. With this control, you can select one item or multiple items. Each selectable item in a CheckBoxList control is considered a ListItem element. This control can be loaded manually through ASP.NET code (like my example) as well as from a data source.
References
Include all the useful links or references that can help users learn about this tutorial
- MSDN: CheckBox List Class
- MSDN: CheckBox and CheckBoxList Web Server Control
Other Related and Popular Articles :
Author Profile : Hans Candra
How would you rate the quality of this content?
Poor
Excellent
Comments
Leave New Comments
Article Content copyright by
Hans Candra
Everything else Copyright © by
WorldofASP.NET
2008