Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select multiple items in asp.net listbox from code

Tags:

c#

asp.net

I have two databound listboxes. The first only shows items that have been assigned to my product. The second listbox shows all available items. What I want to do is select all of the items in listbox 2 that list box one contains.

For example:
ListBox1-
Item 1
Item 3

ListBox2-
Item 1 (Selected)
Item 2
Item 3 (Selected)

Code I have:

List<string> myList = new List<string>();
            foreach(ListItem f in ListBoxSourceDetail.Items)
            {
                myList.Add(f.Value);
            }
            myList.ForEach(delegate(string n)
            {
                ListBoxSourceEdit.SelectedValue = n;
            });
like image 450
The Muffin Man Avatar asked Mar 26 '26 07:03

The Muffin Man


1 Answers

I figured it out, I was over thinking it... Loop through each list item in the first box and then find each matching result in the second table to be selected.

foreach(ListItem i in ListBoxSourceDetail.Items)
        {
            ListBoxSourceEdit.Items.FindByText(i.ToString()).Selected = true;

        }
like image 161
The Muffin Man Avatar answered Mar 27 '26 20:03

The Muffin Man



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!