您的位置:首页 > 其它

ListBox1控件单击选中后自动添加至另一个ListBox2中。

2007-12-26 16:44 337 查看
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindLsb();
}
}
#region 绑定listbox
protected void BindLsb()
{
SqlConnection conn = CDB.Getconn("constr","1");
conn.Open();
CDB.BindListBox(ListBox1, "select * from caes_memberinfo_column_dictionary", "columnname_inchinese", "columnid", conn);
}
#endregion
#region 按钮
//全部增加
protected void Button1_Click(object sender, EventArgs e)
{
ListBox2.Items.Clear();
for (int i=0;i<ListBox1.Items.Count;i++)
{
ListItem Listitem = new ListItem(ListBox1.Items[i].Text, ListBox1.Items[i].Value);
ListBox2.Items.Add(Listitem);
}
}
////增加一项
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string state = "0";
if (ListBox2.Items.Count > 0)
{
for (int j = 0; j < ListBox2.Items.Count; j++)
{
if (ListBox2.Items[j].Value == ListBox1.SelectedItem.Value)
{
state = "1";
ListBox1.SelectedItem.Selected = false;
Response.Write("<script>alert('已经有该选项了,请重新选择!')</script>");
break;
}
}
if (state == "0")
{
ListBox2.Items.Add(ListBox1.SelectedItem);
ListBox2.Items.FindByValue(ListBox1.SelectedItem.Value).Selected = false;
}
}
else
{
ListBox2.Items.Add(ListBox1.SelectedItem);
ListBox2.Items.FindByValue(ListBox1.SelectedItem.Value).Selected = false;
}

}
////删除一项
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected == true)
{
ListBox2.Items.Remove(ListBox2.SelectedItem);
}
}

}
//删除全部
protected void Button4_Click(object sender, EventArgs e)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
ListBox2.Items.Clear();
}
}

#endregion

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐