将多个选定项目从ListBox传输到另一个ListBox

时间:2016-04-13 09:26:35

标签: c# asp.net listbox

我想知道如何将我从左侧ListBox选择的项目移至右侧ListBox?我试图搜索但我只看到将单个项目或所有项目从一个ListBox移动到另一个项目。

3 个答案:

答案 0 :(得分:0)

您可以迭代列表框中的所有项目,检查每个项目是否已选中并将该项目转移到其他列表框

foreach (ListItem item in ListBox1.Items.Where(li=>li.Selected))
{
  // Move item to the other Listbox (You already have the code for this as you mentioned)
}

答案 1 :(得分:0)

首先将Listbox' SelectionMode属性设为Multiple

<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple">

然后:

foreach (ListItem item in ListBox1.Items.Cast<ListItem>().Where(item => item.Selected))
{
    ListBox2.Items.Add(item);
}

ListBox1中按住 Ctrl 键选择多个项目,然后点击该按钮,它应将多个所选项目传输到ListBox2

答案 2 :(得分:0)

我已经解决了我的问题:

foreach (ListItem item in listbox1.Items)
{
  if (item.Selected == true)
  {
    listbox2.Items.Add(item);
  }
}
相关问题