Mikey Mouse
Mikey Mouse

Reputation: 3098

Populating a CheckBoxList with checked options programmatically

I get the feeling I'm missing something really basic about the asp.net page life cycle here.

I have a CheckBoxList (Although the same thing happens with a ListBox)

    <asp:CheckBoxList runat="server" ID="cblCountriesAccessible" DataTextField="Text" />

I'm adding a bunch of ListItems to it

        var lbData = new List<ListItem>();
        lbData.Add(new ListItem("x", "", true));
        lbData.Add(new ListItem("y", "", false));

Then Binding them to the CheckBoxList

    cblCountriesAccessible.DataSource = lbData;
    cblCountriesAccessible.DataBind();

And every time the items in the list show up at unchecked (or unselected in a ListBox) If I add the ListItems directly to the control in the markup, they show up just fine as selected or unselected.

    <asp:ListItem Text="x" Value="" Selected="True" />

What am I doing wrong in the code that selected doesn't carry through? I've tried binding CheckBoxes in with Checked set to true, and the same thing happens.

Edit: OK, I wasn't setting Selected correctly. I'm now using

        var lbData = new List<ListItem>();
        var l1 = new ListItem("yy", "", true) {Selected = true};
        var l2 = new ListItem("xx", "", true) {Selected = false};

        lbData.Add(l1);
        lbData.Add(l2);

And they're still not loading as checked

Upvotes: 2

Views: 27926

Answers (3)

Santoshkumar
Santoshkumar

Reputation: 1

public string GetListItems()
{
    System.Collections.Generic.List<string> items = new System.Collections.Generic.List<string>();

    foreach (ListItem item in lstfunction.Items)
    {
        if (item.Selected)
        {
            items.Add(item.Value);
        }
    }

    return String.Join(", ", items.ToArray());
}

Upvotes: 0

Adil
Adil

Reputation: 148120

You can create ListItem change its Selected property and add in collection.

ListItem li1 = new ListItem("x", "", true);
ListItem li2 = new ListItem("x", "", true); 
li1.Selected = true; 
li2.Selected = true;
cblCountriesAccessible.Items.Add(li1);
cblCountriesAccessible.Items.Add(li2);

Upvotes: 3

Origin
Origin

Reputation: 2023

ListItem.Enabled is not the same as ListItem.Selected . Per this MSDN page ( http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.enabled.aspx )

Enabled simply determines if it COULD be selected. When creating an item with this overload, it does not select it.

Per this page ( http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx ) : "To determine the selected items in the CheckBoxList control, iterate through the Items collection and test the Selected property of each item in the collection."

I have done this in the past using LINQ by having my list of items pre-determined and then iterating through them.

Here's some un-tested code that should give you an idea. This is basically how I did this same thing with Win Forms, so your usage may be slightly different:

For each item in Items
    CheckedListBox.Items.Add(item.Name,Item.value,True)
Next

dim selectedItems as list(of Item) = items.where(function (x) x.selected = True)

for each item in CheckedListBox.items
    item.Selected = selectedItems.contains(item.Value)
next

Upvotes: 2

Related Questions