asp.net中ListBox 绑定多个选项为选中及删除实现方法
导读:收集整理的这篇文章主要介绍了asp.net中ListBox 绑定多个选项为选中及删除实现方法,觉得挺不错的,现在分享给大家,也给大家做个参考。 我们先来看listbox绑定多选项实现 复...
收集整理的这篇文章主要介绍了asp.net中ListBox 绑定多个选项为选中及删除实现方法,觉得挺不错的,现在分享给大家,也给大家做个参考。 我们先来看listbox绑定多选项实现 复制代码 代码如下:
%@ Page Language="C#" %>
!DOCTYPE htML PubLIC "-//W3C//DTD XHTML 1.0 TransITional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
script runat="server">
PRotected void Page_Load(object sender, Eventargs 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="Button1" runat="server" Text="Binding" OnClick="Button1_Click" />
br />
br />
asp:ListBox ID="ListBox1" runat="server" Height="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.DataValUEFIeld = "value";
this.ListBox1.DataBind();
}
private Dictionarystring, string> Site()
{
Dictionarystring, string> site = new Dictionarystring, string> ();
site.Add("Insus.NET cnblogs", 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.baidu.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('; ');
foreach (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 mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- ASP.NET MVC DropDownList数据绑定及使用详解
- ASP.NET Ajax级联DropDownList实现代码
- asp.net省市三级联动的DropDownList+Ajax的三种框架(aspnet/Jquery/ExtJs)示例
- (asp.net c#)DropDownList绑定后显示对应的项的两种方法
- 打造基于jQuery的高性能TreeView(asp.net)
- 关于ASP.NET中TreeView用法的一个小例子
- ASP.NET实现TreeView的XML数据源绑定实例代码
- ASP.NET使用TreeView显示文件的方法
- ASP.NET中使用TreeView显示文件的方法
- ASP.NET中 ListBox列表框控件的使用方法
- ASP.NET中DropDownList和ListBox实现两级联动功能
- Asp.net treeview实现无限级树实现代码
- asp.net实现DropDownList,TreeView,ListBox的无限极分类目录树
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: asp.net中ListBox 绑定多个选项为选中及删除实现方法
本文地址: https://pptw.com/jishu/595150.html