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

  1. <table>  
  2.     <tr>  
  3.         <td>  
  4.             <asp:ListBox ID="leftListBox" runat="server" Width="200" Height="150" SelectionMode="Multiple">  
  5.                 <asp:ListItem Text="广州" Value="广州"></asp:ListItem>  
  6.                 <asp:ListItem Text="深圳" Value="深圳"></asp:ListItem>  
  7.                 <asp:ListItem Text="东莞" Value="东莞"></asp:ListItem>  
  8.                 <asp:ListItem Text="珠海" Value="珠海"></asp:ListItem>  
  9.                 <asp:ListItem Text="长沙" Value="长沙"></asp:ListItem>  
  10.                 <asp:ListItem Text="武汉" Value="武汉"></asp:ListItem>  
  11.                 <asp:ListItem Text="南昌" Value="南昌"></asp:ListItem>  
  12.                 <asp:ListItem Text="南京" Value="南京"></asp:ListItem>  
  13.             </asp:ListBox>  
  14.         </td>  
  15.         <td>  
  16.             <asp:Button ID="btnLeftToRight" runat="server" Text=">" OnClick="btnLeftToRight_Click" /><br />  
  17.             <asp:Button ID="btnRightToLeft" runat="server" Text="<" OnClick="btnRightToLeft_Click" /><br />  
  18.             <asp:Button ID="btnRightUp" runat="server" Text="∧" OnClick="btnRightUp_Click" /><br />  
  19.             <asp:Button ID="btnRightDown" runat="server" Text="∨" OnClick="btnRightDown_Click" /><br />  
  20.         </td>  
  21.         <td>  
  22.             <asp:ListBox ID="rightListBox" runat="server" Width="200" Height="150" SelectionMode="Multiple">  
  23.             </asp:ListBox>  
  24.         </td>  
  25.     </tr>  
  26. </table>  
        <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>


 

  1. //右移   
  2. protected void btnLeftToRight_Click(object sender, EventArgs e)  
  3. {  
  4.     //定义中间动态存储     
  5.     ArrayList arrRight = new ArrayList();  
  6.     //读取左边listbox的item的选中项     
  7.     foreach (ListItem item in this.leftListBox.Items)  
  8.     {  
  9.         if (item.Selected)  
  10.         {  
  11.             arrRight.Add(item);  
  12.         }  
  13.     }  
  14.     //执行右移操作     
  15.     foreach (ListItem item in arrRight)  
  16.     {  
  17.         this.rightListBox.Items.Add(item);  
  18.         this.leftListBox.Items.Remove(item);  
  19.     }    
  20. }  
  21.   
  22. //左移   
  23. protected void btnRightToLeft_Click(object sender, EventArgs e)  
  24. {  
  25.     ArrayList arrLeft = new ArrayList();  
  26.     //读取右边listboxitem的选中项     
  27.     foreach (ListItem item in this.rightListBox.Items)  
  28.     {  
  29.         if (item.Selected)  
  30.         {  
  31.             arrLeft.Add(item);  
  32.         }  
  33.     }  
  34.     //执行左移操作     
  35.     foreach (ListItem item in arrLeft)  
  36.     {  
  37.         this.leftListBox.Items.Add(item);  
  38.         this.rightListBox.Items.Remove(item);  
  39.     }    
  40. }  
  41.   
  42. //上移   
  43. protected void btnRightUp_Click(object sender, EventArgs e)  
  44. {  
  45.     try  
  46.     {  
  47.         if (rightListBox.SelectedIndex > 0)  
  48.         {  
  49.             int idx = rightListBox.SelectedIndex;  
  50.             var SelectedItem = rightListBox.SelectedItem;  
  51.             rightListBox.Items.Insert(rightListBox.SelectedIndex - 1, new ListItem(SelectedItem.Text, SelectedItem.Value));  
  52.             rightListBox.Items.RemoveAt(rightListBox.SelectedIndex);  
  53.             rightListBox.SelectedIndex = idx - 1;  
  54.         }  
  55.     }  
  56.     catch (Exception)  
  57.     {  
  58.         Response.Write("<script language =javascript>alert('请选择元素!')</script>");  
  59.     }    
  60. }  
  61.   
  62. //下移   
  63. protected void btnRightDown_Click(object sender, EventArgs e)  
  64. {  
  65.     try  
  66.     {  
  67.         if (rightListBox.SelectedIndex < rightListBox.Items.Count - 1)  
  68.         {  
  69.             int idx = rightListBox.SelectedIndex;  
  70.             rightListBox.Items.Insert(rightListBox.SelectedIndex, rightListBox.Items[rightListBox.SelectedIndex + 1]);  
  71.             rightListBox.Items.RemoveAt(rightListBox.SelectedIndex + 1);  
  72.             rightListBox.SelectedIndex = idx + 1;  
  73.         }  
  74.     }  
  75.     catch (Exception)  
  76.     {  
  77.         Response.Write("<script language =javascript>alert('请选择元素!')</script>");  
  78.     }    
  79. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值