asp.net中ListBox 綁定多個選項為選中及刪除實現方法

來源:互聯網
上載者:User

我們先來看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", http://www.111cn.net);
        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.111cn.net);
        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綁定多個選項的同時設定多個選中狀態的選項,到最後如何刪除多個己綁定的選項方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.