//效果
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebListBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
//这种方法导致如果连着选两个会导致后边的一个没被选上 (原因:后边的替换前边的索引变了)
//for (int i = 0; i < ListBox1.Items.Count; i++)
//{
// if (ListBox1.Items[i].Selected == true)
// {
// ListBox2.Items.Add(ListBox1.Items[i]);
// ListBox1.Items.Remove(ListBox1.Items[i]);
// }
//}
List<ListItem> list = new List<ListItem>();//讲listbox1的数据暂时存在list中
for (int i = ListBox1.Items.Count-1; i>=0; i--)
{
if (ListBox1.Items[i].Selected == true)
{
list.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
//讲存在listbox1中的数据遍历添加到ListBox2中
for (int i = list.Count-1; i >=0; i--)
{
ListBox2.Items.Add(list[i]);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
}