华域联盟 .Net asp.net中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 Dictionary<string, string> Site()

{

Dictionary<string, string> site = new Dictionary<string, string>();

site.Add("Insus.NET cnblogs", https://www.cnhackhy.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的无限极分类目录树

本文由 华域联盟 原创撰写:华域联盟 » asp.net中ListBox 绑定多个选项为选中及删除实现方法

转载请保留出处和原文链接:https://www.cnhackhy.com/43609.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部