Go through a checked listbox and check all items C#
Asked Answered
R

2

9

I need to loop through a checked listbox, and for each of the items in it, I need to check them (basically like a "select all" function).

Is there a basic example you could give me to help me out please?

Ricard answered 3/4, 2012 at 19:20 Comment(2)
probably you need to explain more...where do these check boxes reside...in a grid? repeater? List?...please add more detail...Tamatave
I agree with NiK .. there are many ways depending on location.. if it is on a webpage.. jQuery is the easiest way :DCystolith
F
22

Use SetSelected and interate through all the Items

// Loop through and set all to selected.
for (int x = 0; x < listBox1.Items.Count; x++)
{
   listBox1.SetSelected(x, true);
}

To check the items, use SetItemChecked

// Loop through and set all to checked.
for (int x = 0; x < listBox1.Items.Count; x++)
{
   listBox1.SetItemChecked(x, true);
}
Fiber answered 3/4, 2012 at 19:24 Comment(1)
thank you very much, this work but only if the line is changed to: listBox1.SetItemChecked(x, true); as it is a checked listboxRicard
P
5

You can look through all the items as ListItems:

foreach (ListItem li in CheckBoxList1.Items)
{
    li.Selected = true;
}
Putamen answered 3/4, 2012 at 19:25 Comment(1)
Anonymous added: (ListItem will require a reference to System.Web.UI.WebControls)Wendalyn

© 2022 - 2024 — McMap. All rights reserved.