ListBox控件基本功能

王朝c#·作者佚名  2006-12-17
宽屏版  字体: |||超大  

ListBox控件基本功能

ListBox基本功能首先是列表项的添加,客户端实现代码添加在listbox实例化代码中间,例如:

<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();

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有