将ListBox1的内容添加到ListBox2.

假设页面中有2个ListBox控件,ID分别为ListBox1 ,ListBox2 和一个Button控件,ID为Btn_Add;ListBox1绑定到一个数据库中,点击Button按钮,将ListBox1选中的值添加到 ListBox2中,原来用的代码是
ListItem li = new ListItem();
li.Items.Text 
= ListBox1.SelectItem.Text.ToString();//为ListBox1中选中的值
li.Items.Value=ListBox1.SelectItem.Value.ToString();//为ListBox1种选中的值的ID

ListBox2.Items.Add(li);
这样既可完成添加.但是由于项目的需要,我把原来所有的文件都该成了UTF-8,这样的话内容就添加不上了,每次添加完后,立即被清除.不知道有没有其他 好的方法解决. 我是这样解决的,在添加的时候把这些值都保存到Session里面,然后在加载页面的时候加载Session里面的值,也就是保存的ListBox2的所 有项的值.我把我的方法写出来.
也适合能用listItem的其他控件.(在<@page>中要加入EnableViewState ="false")


protected   void  Page_Load( object  sender, EventArgs e)
    
{

        
if (Session["Particpants"!= null)
        
{
            ListBox2.Items.Clear();
            ArrayList lstParticpants 
= (ArrayList)Session["Particpants"];
            
int intCount = lstParticpants.Count;
            
for (int i = 0; i < intCount; i++)
            
{
                Contact objRecipient 
= (Contact)lstParticpants[i];
                ListItem li 
= new ListItem();    
                li.Text 
= objRecipient.CB_Text;
                li.Value 
= objRecipient.CB_Value;
                ListBox2.Items.Add(li);
            }

        }

}



 
protected   void  Btn_Add_OnClick( object  sender, EventArgs e) // 点击Button 按钮
     {
ListItem li 
= new ListItem();
li.Items.Text 
= ListBox1.SelectItem.Text.ToString();//为ListBox1中选中的值
li.Items.Value=ListBox1.SelectItem.Value.ToString();//为ListBox1种选中的值的ID
if(ListBox2.Items.Contains(li) == false)
{
ListBox2.Items.Add(li);

                    ArrayList lstParticpants 
= new ArrayList();
                    
int inCount = ListBox2.Items.Count;
                    
if (inCount > 0)
                    
{
                        
for (int i = 0; i < inCount; i++)
                        
{
                            Contact objRecipient 
= new Contact();
                            objRecipient.CB_Value 
= ListBox2.Items[i].Value;
                            objRecipient.CB_Text 
= ListBox2.Items[i].Text;
                            lstParticpants.Add(objRecipient);
                        }

                    }

                    Session[
"Particpants"= lstParticpants;//保存到session中
}

}

public   class  Contact
{
    
private string _CB_Value;
    
public string CB_Value
    
{
        
get
        
{
            
return _CB_Value;

        }

        
set
        
{
            _CB_Value 
= value;
        }

    }

    
private string _CB_Text;
    
public string CB_Text
    
{
        
get
        
{
            
return _CB_Text;

        }

        
set
        
{
            _CB_Text 
= value;
        }

    }

}


    
public  Contact( string  CB_Value,  string  CB_Text)
    
{
        _CB_Value 
= CB_Value;
        _CB_Text 
= CB_Text;
    }

    
public  Contact()
    
{

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值