Asp.net 两个ListBox之间的选项传递(服务器端实现)

2018-11-14  本文已影响0人  葱香排骨面

页面:

<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"  Width="150px" Height="150px" >
                            <asp:ListItem Value="1">One</asp:ListItem>
                            <asp:ListItem Value="2">Two</asp:ListItem>
                            <asp:ListItem Value="3">Three</asp:ListItem>
                        </asp:ListBox>
                    </td>
                    <td  valign="middle" align="center" style="width:100px">
                        <asp:Button ID="ButtonAdd" runat="server"  OnClick="ButtonAdd_Click" Text=">"   Width="50px"/> <br /><br />
                        <asp:Button ID="ButtonRemove" runat="server" OnClick="ButtonAdd_Click" Text="<"   Width="50px"/> <br /><br />
                        <asp:Button ID="ButtonAddAll" runat="server" Text =">>>"    Width="50px"/> <br /><br />
                        <asp:Button ID="ButtonRemoveAll" runat="server" Text ="<<<"   Width="50px"/>
                    </td>

                    <td>
                        <asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple" Width="150px" Height="150px"></asp:ListBox>
                    </td>

                </tr>
            </table>
        </div>
    </form>
</body>

后台:

 Private Sub MoveItems(isAdd As Boolean)
        Dim i As Integer

        If (isAdd) Then
            i = ListBox1.Items.Count - 1

            Do While i >= 0
                If ListBox1.Items(i).Selected Then
                    ListBox2.Items.Add(ListBox1.Items(i))
                    ListBox2.ClearSelection()
                    ListBox1.Items.Remove(ListBox1.Items(i))
                    i = i - 1
                Else
                    i = i - 1
                End If
            Loop
        Else

            i = ListBox2.Items.Count - 1
            Do While i >= 0
                If ListBox2.Items(i).Selected Then
                    ListBox1.Items.Add(ListBox2.Items(i))
                    ListBox1.ClearSelection()
                    ListBox2.Items.Remove(ListBox2.Items(i))
                    i = i - 1
                End If
                i = i - 1
            Loop
        End If

    End Sub

    Private Sub MoveAllItems(isAddAll As Boolean)
        Dim i As Integer

        If (isAddAll) Then
            i = ListBox1.Items.Count - 1

            Do While i >= 0
                ListBox2.Items.Add(ListBox1.Items(i))
                ListBox2.ClearSelection()
                ListBox1.Items.Remove(ListBox1.Items(i))
                i = i - 1
            Loop

        Else
            i = ListBox2.Items.Count - 1

            Do While i >= 0
                ListBox1.Items.Add(ListBox2.Items(i))
                ListBox1.ClearSelection()
                ListBox2.Items.Remove(ListBox2.Items(i))
                i = i - 1
            Loop
        End If

    End Sub

    Protected Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
        Call MoveItems(True)
    End Sub

    Protected Sub ButtonRemove_Click(sender As Object, e As EventArgs) Handles ButtonRemove.Click
        Call MoveItems(False)
    End Sub

    Protected Sub ButtonAddAll_Click(sender As Object, e As EventArgs) Handles ButtonAddAll.Click
        Call MoveAllItems(True)
    End Sub

    Protected Sub ButtonRemoveAll_Click(sender As Object, e As EventArgs) Handles ButtonRemoveAll.Click
        Call MoveAllItems(False)
    End Sub

参考链接:
https://social.msdn.microsoft.com/Forums/sqlserver/ja-JP/93c0b3b1-6f91-4487-aa62-c32db0e9e0a4?forum=aspnetja

上一篇下一篇

猜你喜欢

热点阅读