public partial class 练习 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem li = new ListItem("李娜", "9");
ListBox1.Items.Add(li);
li = new ListItem("易思林", "10");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
#region//单行
//string text = ListBox1.SelectedItem.Text;
//string value = ListBox1.SelectedItem.Value;
//ListItem li = new ListItem(text, value);
//ListBox2.Items.Add(li);
#endregion
foreach (ListItem li in ListBox1.Items)//多行添加
{
if (li.Selected)
{
ListBox2.Items.Add(li);
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
#region//单行
//ListItem li = ListBox2.SelectedItem;
//ListBox2.Items.Remove(li);
#endregion
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)//多行
{
if (ListBox2.Items[i].Selected)
{
ListBox2.Items.RemoveAt(ListBox2.Items.IndexOf(ListBox2.Items[i]));//RemoveAt按索引查找删除;
}
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem li = new ListItem("李娜", "9");
ListBox1.Items.Add(li);
li = new ListItem("易思林", "10");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
#region//单行
//string text = ListBox1.SelectedItem.Text;
//string value = ListBox1.SelectedItem.Value;
//ListItem li = new ListItem(text, value);
//ListBox2.Items.Add(li);
#endregion
foreach (ListItem li in ListBox1.Items)//多行添加
{
if (li.Selected)
{
ListBox2.Items.Add(li);
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
#region//单行
//ListItem li = ListBox2.SelectedItem;
//ListBox2.Items.Remove(li);
#endregion
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)//多行
{
if (ListBox2.Items[i].Selected)
{
ListBox2.Items.RemoveAt(ListBox2.Items.IndexOf(ListBox2.Items[i]));//RemoveAt按索引查找删除;
}
}
}