• 两个ListBox的互动


    适合:
    .Net 2.0
    VS2005
    效果:



    ASPX页面:      
     <table>
       
    <tbody>
          
    <tr>
             
    <td>
                
    <asp:ListBox ID="lbLeft" runat="server" SelectionMode="Multiple">
                   
    <asp:ListItem>添加名字</asp:ListItem>
                   
    <asp:ListItem>出生年月</asp:ListItem>
                
    </asp:ListBox>
             
    </td>
             
    <td style=" 27px">
                
    <asp:Button ID="btnToRight" runat="server" Text=">>"
                        OnClick
    ="btnToRight_Click" />
                
    <br />
                
    <asp:Button ID="btnToLeft" runat="server" Text="<<"
                            OnClick
    ="btnToLeft_Click" />
                
    </td>
             
    <td style=" 3px">
                
    <asp:ListBox ID="lbRight" runat="server"
                        SelectionMode
    ="Multiple"></asp:ListBox></td>
          
    </tr>
       
    </tbody>
    </table>
    <asp:Label ID="lblMsg" runat="server"></asp:Label>

    CS Code :
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;


       
    public partial class Test1 : System.Web.UI.Page
       
    {
          
    protected void Page_Load ( object sender , EventArgs e )
          
    {

          }

          
    protected void btnToRight_Click(object sender, EventArgs e)
          
    {
             
    if(lbLeft.SelectedItem != null)
             
    {
                AddItemFromSourceListBox(lbLeft, lbRight);

                RemoveSelectedItem(lbLeft);

                lblMsg.Text
    =""//注意:为什么要这一行?
                
                
    foreach(ListItem item in lbRight.Items)
                
    {
                   
    if(item.Selected)
                      lblMsg.Text 
    +=item.Text;
                }

             }

          }


          
    protected void btnToLeft_Click(object sender, EventArgs e)
          
    {
             
    if(lbRight.SelectedItem != null)
             
    {            
                AddItemFromSourceListBox(lbRight, lbLeft);
                RemoveSelectedItem(lbRight);
             }

          }


          
    private void RemoveSelectedItem(ListBox listControl)
          
    {         
             
    while(listControl.SelectedIndex != -1)
             
    {
                listControl.Items.RemoveAt(listControl.SelectedIndex);
             }
             
          }


          
    private void AddItemFromSourceListBox(ListBox sourceBox,ListBox targetBox)
          
    {
             
    foreach(ListItem item in sourceBox.Items)
             
    {
                
    if(item.Selected == true && !targetBox.Items.Contains(item))
                
    {
                   targetBox.Items.Add(item);
                }

             }

          }


    }
     



  • 相关阅读:
    Linux下端口被占用确认
    Debuggex – 超好用的正则表达式可视化调试工具
    URL最大长度
    无需重启 修改计算机名直接生效
    UDP穿越NAT原理(p2p)
    gdb中信号
    锁 (读写锁优先级 写饥饿) (锁竞争引发的高系统调用)
    Loopback接口用途---用作管理地址。
    C/C++调试:gdbserver的简单使用
    总结一下NDK crash排查步骤
  • 原文地址:https://www.cnblogs.com/sinkzephyr/p/862632.html
Copyright © 2020-2023  润新知