简体   繁体   English

如何将多个选定的列表框项添加到另一个列表框

[英]How to get Multiple selected listbox items to another listbox

In my ASP.NET application I have two listboxes, say Listbox1 and Listbox2. 在我的ASP.NET应用程序中,我有两个列表框,例如Listbox1和Listbox2。 Listbox1 having some listitems and which is in Multiple Selection Mode. Listbox1有一些列表项,处于多重选择模式。 If I Press the transfer button the selected items in Listbox1 should be moved to Listbox2. 如果我按传输按钮,则Listbox1中的选定项目应移至Listbox2。 I have tried it for Single Selection Move and it works fine. 我已经为单选移动尝试过它,并且效果很好。 Now I need help for multiselection. 现在,我需要多选的帮助。

Code for Single Selection 单选代号

     strItemText = lstAvailableItems.SelectedItem.Text
     iItemCode = lstAvailableItems.SelectedValue
     lstAvailableItems.Items.Remove(New ListItem(strItemText, iItemCode))
     lstSelectedItems.Items.Add(New ListItem(strItemText, iItemCode))

Listbox image 列表框图片

列表框

If I press the > button single selected item will be moved from Available Items listbox to selected items list box. 如果我按>按钮,则单个选定的项目将从“可用项目”列表框移动到“选定的项目”列表框。 How to do this for multiple selection? 如何进行多项选择?

A combination of my original, and @Arman's. 我的原版和@Arman的组合。

    Dim lstRemoveItem As New List(Of ListItem)

    For Each li As ListItem In lstAvailableItems.Items
        If li.Selected Then
            lstRemoveItem.Add(New ListItem(li.Text, li.Value))    
            ' can't remove from the collection while looping through it       
        End If
    Next

    For Each li As ListItem In lstRemoveItem
        lstSelectedItems.Items.Add(li)       ' add to "selected" items
        lstAvailableItems.Items.Remove(li)   ' remove from the original available items
    Next

Use List. 使用清单。 Iterate all items in the listbox and whatever it finds to be selected, add it to the list. 遍历列表框中的所有项目,以及发现要选择的所有项目,将其添加到列表中。

Dim lstRemoveItem As New List(Of ListItem)

For Each _item As ListItem In lstAvailableItems.Items
    If _item.Selected Then
        lstRemoveItem.Add(_item)
        'here, method to add item to the first list.
    End If
Next

For Each _item As ListItem In lstRemoveItem
    lstSelectedItems.Items.Add(_item)
Next

Not exactly what you want, but you can configure the variables easily. 并非完全符合您的要求,但是您可以轻松配置变量。

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

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