您的位置:首页 > 其它

ListBox基本功能

2007-02-25 12:54 218 查看
<asp:ListItem Value="value" Selected=True>Text</asp:ListItem>

若在服务器端实现,为避免每次加载时执行添加列表项,上述代码包含在下面代码中:
if(!IsPostBack)
{
}

WebForm页面上须添加2个listbox(listbox1和lixtbox2)和2个命令按钮,listbox1不为空。列表项从listbox1添加到listbox2须在Button1单击事件中调用Add方法:
ListBox2.Items.Add(ListBox1.SelectedValue);

若要从listbox2中删除列表项的话须在Button2单击事件中调用Remove方法:
ListBox2.Items.Remove(ListBox2.SelectedValue);

列表项从listbox1添加到listbox2后,列表项从listbox1中删除:
int i=0;
while(i<ListBox1.Items.Count)
{
if(ListBox1.Items[i].Selected==true)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
else
i+=1;
}

这样只能实现单项添加,想要实现多项添加,首先设置ListBox1的SelectionMode属性值Multiple,ListBox1允许多项选中。

在Button1单击事件中添加
foreach(ListItem MyItem in ListBox1.Items)
if(MyItem.Selected==true)
ListBox2.Items.Add(MyItem);

想要一次清空ListBox2中所有选项可在Button2单击事件中调用clear方法,
ListBox2.Items.Clear();

若列表项已经添加,不允许二次添加,Button1单击事件中的代码包含在:
if(ListBox2.Items.FindByValue(ListBox1.SelectedValue)==null)
{
}

ListBox与数据库绑定就是指定他的DataSource和DataTextField属性,
ListBox2.DataSource=数据源;
ListBox2.DataTextField="字段名";
ListBox2.DataBind();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: