简体   繁体   English

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

[英]Transfer multi selected items from ListBox to another ListBox

I would like to know how to move the items I have selected from left ListBox to the right ListBox ? 我想知道如何将我选择的项目从左边的ListBox移到右边的ListBox I tried to search but I've only seen moving the single item or all items from one ListBox to another. 我尝试搜索,但只看到将单个项目或所有项目从一个ListBox移到另一个。

You can iterate over all items of the listbox, check each item if it is selected and transfer that item to the other listbox 您可以遍历列表框的所有项目,检查每个项目是否已选中,然后将该项目转移到另一个列表框中

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)
}

Firstly set the Listbox 's SelectionMode property to Multiple : 首先将ListboxSelectionMode属性设置为Multiple

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

Then: 然后:

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

In ListBox1 select multiple items by holding Ctrl key then by clicking the button it should transfer multi selected items to ListBox2 . ListBox1 ,按住Ctrl键选择多个项目,然后单击按钮,它会将多个选定项目传输到ListBox2

I already have solution on my problem: 我已经解决了我的问题:

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

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

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