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>

  上面只是绑定多个了,但我要如何绑定多个选项的同时还选中多个选项呢。

asp.net中ListBox 绑定多个选项为选中及删除实现方法

  .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", http://www.glzy8.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绑定多个选项的同时设置多个选中状态的选项,到最后如何删除多个己绑定的选项方法。