【问题标题】:How to get Multiple selected listbox items to another listbox如何将多个选定的列表框项目获取到另一个列表框
【发布时间】:2014-05-19 20:41:26
【问题描述】:

在我的 ASP.NET 应用程序中,我有两个列表框,比如 Listbox1 和 Listbox2。 Listbox1 有一些列表项并且处于多选模式。如果我按下传输按钮,Listbox1 中的选定项目应移动到 Listbox2。我已经尝试过单选移动,效果很好。 现在我需要多选方面的帮助。

单选代码

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

列表框图片

如果我按 > 按钮,单个选定项目将从可用项目列表框移动到选定项目列表框。多选怎么做?

【问题讨论】:

    标签: asp.net vb.net listbox


    【解决方案1】:

    使用列表。迭代列表框中的所有项目以及任何它发现被选中的项目,将其添加到列表中。

    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
    

    不完全是您想要的,但您可以轻松配置变量。

    【讨论】:

      【解决方案2】:

      我的原件和@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
      

      【讨论】:

      • 它工作得很好,谢谢.. 在我的 lstAvailableItems 我有 4000 个项目。在调试它执行它以获取选定项目时花费的时间太长。有没有其他简单的方法可以做到这一点?
      • @Manivel,GetSelectedIndices 获取当前选定项目的索引值数组,但我不知道它有多快。 Dim selectedValues() As Integer = lstAvailableItems.GetSelectedIndices() 对于这么多项目,通常建议使用不同的用户界面元素(也许让用户按 AR12FC 之类的部分项目搜索,然后从较短的列表中进行选择——但我不是 UI 专家)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多