脚本宝典收集整理的这篇文章主要介绍了asp.net中ListBox 绑定多个选项为选中及删除实现方法,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我们先来看listbox绑定多选项实现
<%@ Page Language="C
#" %>
<!DOCTY
PE ht
ML P
ubLIC "-//W3C//DTD XHTML 1.0 Trans
ITional//EN" "http://www.w3.
org/TR/xhtml1/DTD/xhtml1
-transitional.dtd">
<script runat="server">
PRotected void Page_Load(object sender, Event
args e)
{
for (int i = 0; i < ListBox1.Items.Count;
i++)
{
if (ListBox1.Items[i].Value
== "A" || ListBox1.Items[i].Value == "C")
{
ListBox1.Items[i].Selected = true;
}
}
}
</script>
<html
XMlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>D</asp:ListItem>
<asp:ListItem>E</asp:ListItem>
</asp:ListBox>
</form>
</body>
</html>
上面只是绑定多个了,但我要如何绑定多个选项的同时还选中多个选项呢。
.aspx:
<asp:TextBox ID="TextBox1" runat="server" Width="300"></asp:TextBox>
<
br />
<asp:Button ID="Butto
n1" runat="server" Text="Binding" OnClick="Button1_Click" />
<br />
<br />
<asp:ListBox ID="ListBox1" runat="server" H
eight="100" SelectionMode="Multiple" ></asp:ListBox> .
aspx.cs中,首先是为ListBox准备数据,然后对ListBox控件进行数据绑定:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
this.ListBox1.DataSource = Site();
this.ListBox1.DataTextField = "key";
this.ListBox1.DataVal
UEFIeld = "value";
this.ListBox1.DataBind();
}
private Dictionary<string, string> Site()
{
Dictionary<string, string> site = new Dictionary<string, string>();
site.Add("Insus.NET cn
blogs", https://www.js-code
.COM);
site.Add("Microsoft", "http://www.microsoft.com");
site.Add("
GOOGLE", "http://www.google.com");
site.Add("Yahoo", "http://www.yahoo.com.cn");
site.Add("Ifeng", "http://www.ifeng.com");
site.Add("sina", http://www.b
aidu.com);
site.Add("163", "http://www.163.com");
site.Add("
QQ", "http://www.qq.com");
return site;
}
为了让TextBox的字符串以";"分割为多个值,引用了命名空间
using
System.Collections; 接下来,是写button的click事件,代码相当
简单,Insus.NET在此不作过多注释:
protected void Button1_Click(object sender, EventArgs e)
{
string[] s = this.TextBox1.Text.Split(';');
for
each (ListItem li in this.ListBox1.Items)
{
li.Selected = ((IList)s).Contains(li.Text) ? true : false;
}
}
最后我们来看看如何删除listbox的多个选项的方法
删除多个选项
int coun =listBox2.SelectedItems.Count;
for(;coun> =1;coun--)
{
listBox2.Items.RemoveAt(listBox2.SelectedIndices[coun-1]);
}
listBox2.ClearSelected();
}
一
网友回复
foreach( object d in listBox2.SelectedItems)
这一行有问题,你
知道,当你删除其中一个选项时,listBOx的所选项已经被更改了,你再调用foreach,当然会有问题!
解决方法是将listBox2.SelectedItems先放入一个数组变量再行调用就没问题了!
不过当然更简单的方法就是直接调用
listBox2.ClearSelected();
是的,这一句话就解决了所有的问题!所有的被选择项目都会被清除掉
总结 本
文章讲述了listbox从绑定多个选项和listbox绑定多个选项的同时设置多个选中状态的选项,到最后如何删除多个己绑定的选项方法。
脚本宝典总结
以上是脚本宝典为你收集整理的asp.net中ListBox 绑定多个选项为选中及删除实现方法全部内容,希望文章能够帮你解决asp.net中ListBox 绑定多个选项为选中及删除实现方法所遇到的问题。
如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。