asp.net中ListBox 綁定多個選項為選中及刪除實現方法
更新時間:2012年04月28日 15:18:07 作者:
文章介紹了關于在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", http://www.dbjr.com.cn);
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綁定多個選項的同時設置多個選中狀態(tài)的選項,到最后如何刪除多個己綁定的選項方法。
復制代碼 代碼如下:
<%@ 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.dbjr.com.cn);
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綁定多個選項的同時設置多個選中狀態(tài)的選項,到最后如何刪除多個己綁定的選項方法。
您可能感興趣的文章:
- 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的無限極分類目錄樹
相關文章
在?Net7.0?環(huán)境下如何使用?RestSharp?發(fā)送?Http(FromBody和FromForm)請求
這篇文章主要介紹了在?Net7.0?環(huán)境下使用?RestSharp?發(fā)送?Http(FromBody和FromForm)請求,今天,我就兩個小的知識點,就是通過使用?RestSharp?訪問?WebAPI,提交?FromBody?和?FromForm?兩種方式的數據,還是有些區(qū)別的,本文結合實例代碼介紹的非常詳細,需要的朋友參考下吧2023-09-09完美兼容ie和firefox的asp.net網站加入收藏和設置主頁
這篇文章主要介紹了完美兼容ie和firefox的asp.net網站加入收藏和設置主頁,需要的朋友可以參考下2014-12-12.NET?6開發(fā)TodoList應用之使用MediatR實現POST請求
對于稍微正式的項目,.NET工程上習慣的實現是通過使用比較成熟的類庫框架,有效地對業(yè)務邏輯進行分類管理、消除冗余代碼,以達到業(yè)務邏輯職責清晰簡潔的目的。在這個階段我們經常使用的兩個類庫分別是AutoMapper和MediatR。本文將為大家介紹MediatR如何實現POST請求2021-12-12ASP.NET?MVC創(chuàng)建XML文件并實現元素增刪改
這篇文章介紹了ASP.NET?MVC創(chuàng)建XML文件并實現元素增刪改的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07