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

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

作者: 葱香排骨面 | 来源:发表于2018-11-14 01:10 被阅读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

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/fbarfqtx.html