两个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 = " width: 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 = " width: 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);
            }

         }

      }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值