13

I have a asp.net checkbox list bound to a linq to sql datasource and when I check the item count of my CheckBoxList on the page load event it is 0. I want to be able to set the selected items on my checkboxlist here but cannot.

The datasource's selected event fires after the page load. If this is the case, how can I set the selected items on my list?

If I set the SelectedValue to a value it only displays the last value as selected instead of all the values which are selected. How can I select multiple values in my checkboxlist in the pageload event?

7 Answers 7

11

I know this is an old post but I had the same problem recently.

To select multiple items of a DataBound CheckBoxList, handle the DataBound event and loop through the Items collection setting the Selected property individually on each item as required.

Setting the SelectedValue property of the control only checks the final item.

 foreach (ListItem item in MyCheckBoxList.Items)
 {
     item.Selected = ShouldItemBeSelectedMethod(item.Value);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

don't forget to include the System.Web.UI.WebControls namespace
6

Nice method I use:

 private void SetCheckBoxListValues(CheckBoxList cbl, string[] values)
        {
            foreach (ListItem item in cbl.Items)
            {
                item.Selected = values.Contains(item.Value);
            }
        }

2 Comments

This will throw an error unless you have: using System.Linq;
For me, cbl.Items is empty at Page_Load.
3
public void SetValueCheckBoxList(CheckBoxList cbl, string sValues)
        {
            if (!string.IsNullOrEmpty(sValues))
            {                
                ArrayList values = StringToArrayList(sValues);             
                foreach (ListItem li in cbl.Items)
                {
                    if (values.Contains(li.Value))
                        li.Selected = true;
                    else
                        li.Selected = false;                    
                }               
            }
        }

private ArrayList StringToArrayList(string value)
        {
            ArrayList _al = new ArrayList();
            string[] _s = value.Split(new char[] { ',' });

            foreach (string item in _s)
                _al.Add(item);

            return _al;
        }

Thanks, slnavn2000

Comments

1

Sounds like a Page Lifecycle - Databinding question.

You should really take a look at (the answers to) this question.

Comments

1

I used the DataBound event to select to set the selected items.

Comments

1

Lets say your values are string array. Then I would do it this way

foreach (ListItem li in ctrl.Items)
   li.Selected = Array.Exists(values, x => x == li.Value);

Comments

0

set checkboxlist selected items from a list:

        List<int> yourlist;
        //fill yourlist
        foreach (ListItem item in checkboxlist.Items)
        {
            if (yourlist.Contains(int.Parse(item.Value.ToString())))
                item.Selected = true;                
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.