假设页面中有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")
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()
{
}
{
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()
{
}