asp.net中两个ListBox 左右互相移动,以及上移、下移

<table>
            <tr>
                <td>
                    <asp:ListBox ID="leftListBox" runat="server" Width="200" Height="150" SelectionMode="Multiple">
                        <asp:ListItem Text="广州" Value="广州"></asp:ListItem>
                        <asp:ListItem Text="深圳" Value="深圳"></asp:ListItem>
                        <asp:ListItem Text="东莞" Value="东莞"></asp:ListItem>
                        <asp:ListItem Text="珠海" Value="珠海"></asp:ListItem>
                        <asp:ListItem Text="长沙" Value="长沙"></asp:ListItem>
                        <asp:ListItem Text="武汉" Value="武汉"></asp:ListItem>
                        <asp:ListItem Text="南昌" Value="南昌"></asp:ListItem>
                        <asp:ListItem Text="南京" Value="南京"></asp:ListItem>
                    </asp:ListBox>
                </td>
                <td>
                    <asp:Button ID="btnLeftToRight" runat="server" Text=">" OnClick="btnLeftToRight_Click" /><br />
                    <asp:Button ID="btnRightToLeft" runat="server" Text="<" OnClick="btnRightToLeft_Click" /><br />
                    <asp:Button ID="btnRightUp" runat="server" Text="∧" OnClick="btnRightUp_Click" /><br />
                    <asp:Button ID="btnRightDown" runat="server" Text="∨" OnClick="btnRightDown_Click" /><br />
                </td>
                <td>
                    <asp:ListBox ID="rightListBox" runat="server" Width="200" Height="150" SelectionMode="Multiple">
                    </asp:ListBox>
                </td>
            </tr>
        </table>


 

//右移
    protected void btnLeftToRight_Click(object sender, EventArgs e)
    {
        //定义中间动态存储  
        ArrayList arrRight = new ArrayList();
        //读取左边listbox的item的选中项  
        foreach (ListItem item in this.leftListBox.Items)
        {
            if (item.Selected)
            {
                arrRight.Add(item);
            }
        }
        //执行右移操作  
        foreach (ListItem item in arrRight)
        {
            this.rightListBox.Items.Add(item);
            this.leftListBox.Items.Remove(item);
        }  
    }

    //左移
    protected void btnRightToLeft_Click(object sender, EventArgs e)
    {
        ArrayList arrLeft = new ArrayList();
        //读取右边listboxitem的选中项  
        foreach (ListItem item in this.rightListBox.Items)
        {
            if (item.Selected)
            {
                arrLeft.Add(item);
            }
        }
        //执行左移操作  
        foreach (ListItem item in arrLeft)
        {
            this.leftListBox.Items.Add(item);
            this.rightListBox.Items.Remove(item);
        }  
    }

    //上移
    protected void btnRightUp_Click(object sender, EventArgs e)
    {
        try
        {
            if (rightListBox.SelectedIndex > 0)
            {
                int idx = rightListBox.SelectedIndex;
                var SelectedItem = rightListBox.SelectedItem;
                rightListBox.Items.Insert(rightListBox.SelectedIndex - 1, new ListItem(SelectedItem.Text, SelectedItem.Value));
                rightListBox.Items.RemoveAt(rightListBox.SelectedIndex);
                rightListBox.SelectedIndex = idx - 1;
            }
        }
        catch (Exception)
        {
            Response.Write("<script language =javascript>alert('请选择元素!')</script>");
        }  
    }

    //下移
    protected void btnRightDown_Click(object sender, EventArgs e)
    {
        try
        {
            if (rightListBox.SelectedIndex < rightListBox.Items.Count - 1)
            {
                int idx = rightListBox.SelectedIndex;
                rightListBox.Items.Insert(rightListBox.SelectedIndex, rightListBox.Items[rightListBox.SelectedIndex + 1]);
                rightListBox.Items.RemoveAt(rightListBox.SelectedIndex + 1);
                rightListBox.SelectedIndex = idx + 1;
            }
        }
        catch (Exception)
        {
            Response.Write("<script language =javascript>alert('请选择元素!')</script>");
        }  
    }


 

转载于:https://my.oschina.net/smartsmile/blog/815403

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值