Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Asp.net checkboxlist control - Part 23

Suggested Videos
Part 16 - Dropdownlist in asp.net
Part 17 - Data bind dropdownlist with data from the database
Part 21 - Retrieving selected item text, value and index of the dropdownlist

In this video we will learn about asp.net checkboxlist control. Just like DropDownList
1. CheckBoxList is collection of ListItem objects.
2. Items can be added to the CheckBoxList in the HTML source or in the code behind file
3. CheckBoxList can be bound to a database table or an xml file

DropDownList is generally used, when you want to present the user with multiple choices, from which you want him to select only one option. Where as if you want the user to select more than one option, then a CheckBoxList control can be used.

Create an asp.net web application. Copy and paste the following HTML
<asp:CheckBoxList ID="checkboxListEducation" runat="server" 
            RepeatDirection="Horizontal">
    <asp:ListItem Text="Diploma" Value="1"></asp:ListItem>
    <asp:ListItem Text="Graduate" Value="2"></asp:ListItem>
    <asp:ListItem Text="Post Graduate" Value="3"></asp:ListItem>
    <asp:ListItem Text="Doctrate" Value="4"></asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />



Copy and paste the following code in Button1_Click event. The sample code prints the Text, Value and Index of the selected list item object.
// Loop thru each list item in the checkboxlist
foreach (ListItem li in checkboxListEducation.Items)
{
    // If the list item is selected
    if (li.Selected)
    {
        // Retrieve the text of the selected list item
        Response.Write("Text = " + li.Text + ", ");
        // Retrieve the value of the selected list item
        Response.Write("Value = " + li.Value + ", ");
        // Retrieve the index of the selected list item
        Response.Write("Index = " + checkboxListEducation.Items.IndexOf(li).ToString());
        Response.Write("<br/>");
    }
}



By default, the ListItem objects are laid out in vertical direction. If you want to change the direction, use RepeatDirection property
<asp:CheckBoxList ID="checkkboxListEducation" runat="server" RepeatDirection="Horizontal">

RepeatColumns property specifies the number of columns used to lay out the items.

Set the Enabled property of the ListItem object to false, to disable the selection, in the CheckBoxList control.

SelectedIndex property of the CheckBoxList control can also be used to get the index of the selected item in the checkboxlist. But this property, returns only one selected item, and that too, the item with the lowest index. SelectedIndex property returns -1, if nothing is selected.

SelectedValue property returns the selected Item's value, but only for one selected item. If no item is selected this property returns empty string.

To retrieve the Text of the selected item, SelectedItem.Text property can be used. SelectedItem will be NULL, if nothing is selected, and hence, calling Text and Value properties may cause NullReferenceException. Hence, it is important to check for null, when using SelectedItem property of a CheckBoxList control.
if (checkboxListEducation.SelectedItem != null)
{
    Response.Write(checkboxListEducation.SelectedItem.Text);
}

3 comments:

  1. Hi Venkat,
    thank you for your excellent teaching. After watching this video I thought the SelectedIndex property could be used as an additional condition to start the foreach loop in the Button1_Click event. In this case, if no item is selected, no iteration will be performed:
    ...
    protected void Button1_Click(object sender, EventArgs e)
    {
    // This is the added test:
    if (checkboxListEducation.SelectedIndex < 0) return;

    // Loop thru each list item in the checkboxlist
    foreach (ListItem li in checkboxListEducation.Items)
    {
    // If the list item is selected
    if (li.Selected)
    ... etc.

    ReplyDelete
  2. Hi Venkat,
    I have improved the idea that I sent yesterday. Here it is: to further improve the performance in the Button1_Click event procedure, we can use the for-next loop with start index equals to the SelectedIndex property. With this approach in a list of 1000 elements in which the only element selected is the last element, only one iteration will be performed and not 1000 as in the case of the for-each approach.
    Here is the code:

    protected void Button1_Click(object sender, EventArgs e)
    {
    int iFirstSel = checkboxListEducation.SelectedIndex;

    if (iFirstSel < 0) return;

    for (int i = iFirstSel; i < checkboxListEducation.Items.Count; i++)
    {
    // If the list item is selected
    if (checkboxListEducation.Items[i].Selected)
    {
    // Retrieve the text of the selected list item
    Response.Write("Text = " + checkboxListEducation.Items[i].Text + ", ");
    // Retrieve the value of the selected list item
    Response.Write("Value = " + checkboxListEducation.Items[i].Value + ", ");
    // Retrieve the index of the selected list item
    Response.Write("Index = " + i.ToString());
    Response.Write("
    ");
    }
    }
    }

    ReplyDelete
  3. hi bro
    code nice but databasestored retrive checkbox

    ReplyDelete

It would be great if you can help share these free resources