1:注意要得把两个listbox的selectectionmode属性设置为mutiple
2 后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class TestListBox : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ListBox2Bind(); } } private void ListBox2Bind() { string[] str = new string[] { "北京","上海","福建","广东","浙江" }; for (int i = 0; i < str.Length - 1; i++) { ListBox2.Items.Add(str[i]); } } protected void btnAdd_Click(object sender, EventArgs e) { int count = ListBox2.Items.Count; int index = 0; for (int i = 0; i < count; i++) { ListItem item = ListBox2.Items[index]; if (ListBox2.Items[index].Selected == true) { ListBox2.Items.Remove(item); ListBox3.Items.Add(item); index--; } index++; } } protected void btnDel_Click(object sender, EventArgs e) { int count = ListBox3.Items.Count; int index = 0; for (int i = 0; i < count; i++) { ListItem item = ListBox3.Items[index]; if (ListBox3.Items[index].Selected == true) { ListBox3.Items.Remove(item); ListBox2.Items.Add(item); index--; } index++; } } protected void AddAll_Click(object sender, EventArgs e) { int count = ListBox2.Items.Count; int index = 0; for (int i = 0; i < count; i++) { ListItem item = ListBox2.Items[index]; ListBox2.Items.Remove(item); ListBox3.Items.Add(item); } index++; } protected void DelAll_Click(object sender, EventArgs e) { int count = ListBox3.Items.Count; int index = 0; for (int i = 0; i < count; i++) { ListItem item = ListBox3.Items[index]; ListBox3.Items.Remove(item); ListBox2.Items.Add(item); } index++; } }
3:效果图