ASP.NET(VB)列表框未将所有选定项移动到其他列表框 [英] ASP.NET(VB) Listboxes are not moving all the selected items to other Listboxes

查看:50
本文介绍了ASP.NET(VB)列表框未将所有选定项移动到其他列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DivisionListBox用于在SitesListbox中选择值,如下所示:当您在DivisionListBox中选择一个项目时,它将选择SitesListbox中与该值匹配的所有项目.

The DivisionListBox is used to select values in the SitesListbox as follows: When you select an item in the DivisionListBox it will select all the items on SitesListbox that match the same value.

一旦选择了SiteListbox的值,我将使用SiteButton将所有选定的值成功移动到空的StoreslistBox.

Once the SiteListbox values are selected I use the SiteButton to move all selected values to the empty StoreslistBox successfully.

当我在SiteListbox或StoreslistBox上手动选择多个项目并按SiteButton或StoreButton在列表框之间移动项目时,会发生我的问题.它既可以一次移动一个项目,也可以移动一些选定的项目,但从不移动所有选定的项目(可以通过标签跟踪).

My problem happens when I manually select multiple items on either SiteListbox or StoreslistBox and press SiteButton or StoreButton to move items between Listboxes. It either moves one item at a time or some of the selected items but it never moves all of the selected items (which can be track by the labels).

是什么原因导致此问题?对此有什么可能的解决方案?

What is causing this problem and what could be a possible solution for this?

我的代码

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    DivisionSelection()

End Sub

Private Sub DivisionSelection()

    If DivisionListBox.SelectedValue IsNot Nothing Then
        SitesListBox.SelectionMode = ListSelectionMode.Multiple
        For Each item As ListItem In SitesListBox.Items
            If item.Value = DivisionListBox.SelectedValue Then
                item.Selected = True
            End If
        Next
    End If

    Dim x As Integer = 0
    Dim Siteitem As ListItem
    For Each Siteitem In SitesListBox.Items
        If Siteitem.Selected Then
            x += 1
            Label1.Text = x
        End If
    Next
End Sub

Protected Sub SiteButton_Click(sender As Object, e As EventArgs) Handles SiteButton.Click

    StoresListBox.SelectionMode = ListSelectionMode.Multiple
    While SitesListBox.SelectedItem IsNot Nothing
        StoresListBox.Items.Add(SitesListBox.SelectedItem)
        SitesListBox.Items.Remove(SitesListBox.SelectedItem)
    End While

    'counts the selected items
    Dim x As Integer = 0
    Dim Siteitem As ListItem
    For Each Siteitem In SitesListBox.Items
        If Siteitem.Selected Then
            x += 1
            Label1.Text = x
        End If
    Next
End Sub

Protected Sub StoreButton_Click(sender As Object, e As EventArgs) Handles StoreButton.Click

    StoresListBox.SelectionMode = ListSelectionMode.Multiple
    While StoresListBox.SelectedItem IsNot Nothing
        SitesListBox.Items.Add(StoresListBox.SelectedItem)
        StoresListBox.Items.Remove(StoresListBox.SelectedItem)
    End While

    'counts the selected items
    Dim x As Integer = 0
    Dim Storeitem As ListItem
    For Each Storeitem In SitesListBox.Items
        If Storeitem.Selected Then
            x += 1
            Label2.Text = x
        End If
    Next

End Sub

推荐答案

我将所选项目添加到列表中;然后遍历列表进行添加和删除.当一个集合或索引在一个循环中混乱时,它可能会产生意想不到的结果.

I added the selected items to a list; then looped through the list to do the adding and removing. When a collection or indexes are messed with inside a loop it can have unexpected results.

Protected Sub Button1_Click(ByVal sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim lstItems As New List(Of ListItem)
    For Each item As ListItem In ListBox1.Items
        If item.Selected Then
            lstItems.Add(item)
        End If
    Next
    'It wasn't clear if you wanted the items to remain selected after moving.
    ListBox1.ClearSelection()
    Debug.Print(lstItems.Count.ToString)
    For Each item As ListItem In lstItems
        ListBox2.Items.Add(item)
        ListBox1.Items.Remove(item)
    Next
End Sub

这篇关于ASP.NET(VB)列表框未将所有选定项移动到其他列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆