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按索引查找删除;
}
}
}
本文介绍了一个ASP.NET中ListBox控件的操作实例,包括如何在页面加载时向ListBox添加项,以及通过按钮点击事件实现从一个ListBox向另一个ListBox批量转移选定项的功能。
4260

被折叠的 条评论
为什么被折叠?



