最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
ListBox控件基本功能
时间:2008-01-12 编辑:简简单单 来源:一聚教程网
若在服务器端实现,为避免每次加载时执行添加列表项,上述代码包含在下面代码中:
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
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();
-
上一个: 一个数字文本框控件,我觉得思路蛮不错的
-
下一个: 在RichTextBox控件加入图片
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10