简体   繁体   English

从代码中选择asp.net列表框中的多个项目

[英]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 select all of the items in listbox 2 that list box one contains. 我想要做的是选择列表框2中包含的列表框2中的所有项目。

For example: 例如:
ListBox1- ListBox1-
Item 1 第1项
Item 3 第3项

ListBox2- ListBox2-
Item 1 (Selected) 第1项(选定)
Item 2 第2项
Item 3 (Selected) 第3项(选定)

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;

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM