select multiple items in asp.net listbox from code
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开发者_如何学Python 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;
});
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;
}
精彩评论