WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 186 users


WorldofASP.NET >> ASP.NET >> Unedited ASP.NET

Working with CheckBox List Control in ASP.NET

Appending, removing, binding List Item with CheckBox List Control
Published Date : 13 Aug 2008
Author : Hans Candra
Language : C#
Platform : .NET
Technology : ASP.NET
Views : 4314
Rating : (0 votes so far)



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

  1. MSDN: CheckBox List Class
  2. MSDN: CheckBox and CheckBoxList Web Server Control

Tag Cloud
  upload file progress bar asp.net   asp.net pass data from one page to another   formview displaying inserted data   httpwebrequest create   contact us asp.net   formview asp.net   asp httpwebrequest   asp.net cookies encryption   asp.net url parameters   datalist control   asp.net delegate   cannot start service from the command line or a debugger. a windows service must   asp.net cookie shopping cart   httpwebrequest in asp.net 2.0   checkboxlist datasource   httpwebrequest asp.net c#   ajax updatepanel button   cannot start service from the command line or a debugger   form view in asp.net   httpwebresponse   httpwebrequest httpwebresponse   tooltip asp.net   httpwebrequest vb.net   httpwebrequest .net   create httpwebrequest   asp.net file upload progress bar   "javascript in asp.net"   feedback form in asp.net   smart device application   generate random number asp.net   asp.net tooltip   asp.net file upload progress   frames image gallery thumbnails asp.net   asp.net upload ajax progress   asp.net listbox control   edit update insert in a gridview c#   httpmodule httphandler   httpwebrequest   asp.net random number   asp.net encryption   asp.net httpwebrequest





Other Related and Popular Articles :

Simple asp2 feedback form
A simple way to get user feedback via webpage email.

Designing a Membership Sign-Up and Login Page in ASP.NET
How to design user interface for registration and login in ASP.NET

Validate Email and Domain in a Web Form
Simple method to validate using javascript and ASP.NET validation control

Basics of XHTML-MP
Basics of XHTML-MP

Activate User Registration by Email in ASP.NET
How to activate registered user using System.Net.Mail.SMTPClient class

Implement captcha control library for ASP.NET
a captcha control, image, image handler, and supression class for form validation


Author Profile : Hans Candra

Click here to view Author Profile


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

Category
.NET 3.5
AJAX and ATLAS
ASP.NET
C# Programming
Classic ASP
Enterprise Systems
General .NET
VB.NET Programming
Announcements
Earn Cash by writing an article or review
For more info Click here







Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card