zoukankan      html  css  js  c++  java
  • asp.net 两个listbox交互

    两个ListBox的互动方法

    效果:



    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);
                }

             }

          }


    }
     

  • 相关阅读:
    The Python Standard Library
    Python 中的round函数
    Python文件类型
    Python中import的用法
    Python Symbols 各种符号
    python 一行写多个语句
    免费SSL证书(https网站)申请,便宜SSL https证书申请
    元宇宙游戏Axie龙头axs分析
    OLE DB provider "SQLNCLI10" for linked server "x.x.x.x" returned message "No transaction is active.".
    The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "xxx.xxx.xxx.xxx" was unable to begin a distributed transaction.
  • 原文地址:https://www.cnblogs.com/relang99/p/979129.html
Copyright © 2011-2022 走看看