asp.net中ListBox 綁定多個(gè)選項(xiàng)為選中及刪除實(shí)現(xiàn)方法
更新時(shí)間:2012年04月28日 15:18:07 作者:
文章介紹了關(guān)于在asp.net中的listbox的綁定多個(gè)選項(xiàng)和同時(shí)選中多個(gè)選項(xiàng)以及刪除多個(gè)選項(xiàng)的方法
我們先來看listbox綁定多選項(xiàng)實(shí)現(xiàn)
<%@ 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>
上面只是綁定多個(gè)了,但我要如何綁定多個(gè)選項(xiàng)的同時(shí)還選中多個(gè)選項(xiàng)呢。

.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中,首先是為L(zhǎng)istBox準(zhǔn)備數(shù)據(jù),然后對(duì)ListBox控件進(jìn)行數(shù)據(jù)綁定:
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.fzitv.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.baidu.com);
site.Add("163", "http://www.163.com");
site.Add("QQ", "http://www.qq.com");
return site;
}
為了讓TextBox的字符串以";"分割為多個(gè)值,引用了命名空間
using System.Collections; 接下來,是寫button的click事件,代碼相當(dāng)簡(jiǎn)單,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的多個(gè)選項(xiàng)的方法
刪除多個(gè)選項(xiàng)
int coun =listBox2.SelectedItems.Count;
for(;coun> =1;coun--)
{
listBox2.Items.RemoveAt(listBox2.SelectedIndices[coun-1]);
}
listBox2.ClearSelected();
}
一網(wǎng)友回復(fù)
foreach( object d in listBox2.SelectedItems)
這一行有問題,你知道,當(dāng)你刪除其中一個(gè)選項(xiàng)時(shí),listBOx的所選項(xiàng)已經(jīng)被更改了,你再調(diào)用foreach,當(dāng)然會(huì)有問題!
解決方法是將listBox2.SelectedItems先放入一個(gè)數(shù)組變量再行調(diào)用就沒問題了!
不過當(dāng)然更簡(jiǎn)單的方法就是直接調(diào)用
listBox2.ClearSelected();
是的,這一句話就解決了所有的問題!所有的被選擇項(xiàng)目都會(huì)被清除掉
總結(jié)
本文章講述了listbox從綁定多個(gè)選項(xiàng)和listbox綁定多個(gè)選項(xiàng)的同時(shí)設(shè)置多個(gè)選中狀態(tài)的選項(xiàng),到最后如何刪除多個(gè)己綁定的選項(xiàng)方法。
復(fù)制代碼 代碼如下:
<%@ 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>
上面只是綁定多個(gè)了,但我要如何綁定多個(gè)選項(xiàng)的同時(shí)還選中多個(gè)選項(xiàng)呢。

.aspx:
復(fù)制代碼 代碼如下:
<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中,首先是為L(zhǎng)istBox準(zhǔn)備數(shù)據(jù),然后對(duì)ListBox控件進(jìn)行數(shù)據(jù)綁定:
復(fù)制代碼 代碼如下:
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.fzitv.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.baidu.com);
site.Add("163", "http://www.163.com");
site.Add("QQ", "http://www.qq.com");
return site;
}
為了讓TextBox的字符串以";"分割為多個(gè)值,引用了命名空間
using System.Collections; 接下來,是寫button的click事件,代碼相當(dāng)簡(jiǎn)單,Insus.NET在此不作過多注釋:
復(fù)制代碼 代碼如下:
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的多個(gè)選項(xiàng)的方法
刪除多個(gè)選項(xiàng)
復(fù)制代碼 代碼如下:
int coun =listBox2.SelectedItems.Count;
for(;coun> =1;coun--)
{
listBox2.Items.RemoveAt(listBox2.SelectedIndices[coun-1]);
}
listBox2.ClearSelected();
}
一網(wǎng)友回復(fù)
foreach( object d in listBox2.SelectedItems)
這一行有問題,你知道,當(dāng)你刪除其中一個(gè)選項(xiàng)時(shí),listBOx的所選項(xiàng)已經(jīng)被更改了,你再調(diào)用foreach,當(dāng)然會(huì)有問題!
解決方法是將listBox2.SelectedItems先放入一個(gè)數(shù)組變量再行調(diào)用就沒問題了!
不過當(dāng)然更簡(jiǎn)單的方法就是直接調(diào)用
listBox2.ClearSelected();
是的,這一句話就解決了所有的問題!所有的被選擇項(xiàng)目都會(huì)被清除掉
總結(jié)
本文章講述了listbox從綁定多個(gè)選項(xiàng)和listbox綁定多個(gè)選項(xiàng)的同時(shí)設(shè)置多個(gè)選中狀態(tài)的選項(xiàng),到最后如何刪除多個(gè)己綁定的選項(xiàng)方法。
您可能感興趣的文章:
- asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
- ASP.NET Ajax級(jí)聯(lián)DropDownList實(shí)現(xiàn)代碼
- asp.net省市三級(jí)聯(lián)動(dòng)的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- (asp.net c#)DropDownList綁定后顯示對(duì)應(yīng)的項(xiàng)的兩種方法
- 打造基于jQuery的高性能TreeView(asp.net)
- 關(guān)于ASP.NET中TreeView用法的一個(gè)小例子
- ASP.NET實(shí)現(xiàn)TreeView的XML數(shù)據(jù)源綁定實(shí)例代碼
- ASP.NET使用TreeView顯示文件的方法
- ASP.NET中使用TreeView顯示文件的方法
- ASP.NET中 ListBox列表框控件的使用方法
- ASP.NET中DropDownList和ListBox實(shí)現(xiàn)兩級(jí)聯(lián)動(dòng)功能
- Asp.net treeview實(shí)現(xiàn)無限級(jí)樹實(shí)現(xiàn)代碼
- asp.net實(shí)現(xiàn)DropDownList,TreeView,ListBox的無限極分類目錄樹
相關(guān)文章
ASP.net實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法
在ASP.NET進(jìn)行頁面開發(fā)時(shí)候經(jīng)常遇到需要進(jìn)行頁面跳轉(zhuǎn)的操作。這個(gè)其實(shí)并不難,關(guān)鍵是知不知道的問題。下面給出操作方法。2013-06-06
如何給asp.net core寫個(gè)簡(jiǎn)單的健康檢查
這篇文章主要給大家介紹了關(guān)于如何給asp.net core寫個(gè)簡(jiǎn)單的健康檢查的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用asp.net core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
asp.net開發(fā)與web標(biāo)準(zhǔn)的沖突問題的一些常見解決方法
Visual Studio .net從2003到現(xiàn)在的2008,一路走來慢慢強(qiáng)大……從以前的vs2003能自動(dòng)改亂你的html代碼到現(xiàn)在在vs2008中都能直接對(duì)html代碼進(jìn)行w3c標(biāo)準(zhǔn)驗(yàn)證并提示了,非常不易。2009-02-02
asp.net中一次性動(dòng)態(tài)綁定多個(gè)droplistdown
asp.net中一次性動(dòng)態(tài)綁定多個(gè)droplistdown的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-10-10
Asp.net Socket客戶端(遠(yuǎn)程發(fā)送和接收數(shù)據(jù))
通過Socket遠(yuǎn)程發(fā)送與接收數(shù)據(jù)的代碼類2008-11-11
.Net極限生產(chǎn)力之分表分庫全自動(dòng)化Migrations?Code-First
這篇文章主要介紹了.Net極限生產(chǎn)力之分表分庫全自動(dòng)化Migrations?Code-First,輕量級(jí)針對(duì)分表分庫讀寫分離的解決方案,具有零依賴、零學(xué)習(xí)成本、零業(yè)務(wù)代碼入侵適配2022-07-07
asp.net實(shí)現(xiàn)拒絕頻繁的IP訪問的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)拒絕頻繁的IP訪問的方法,涉及asp.net針對(duì)訪問IP的判斷及配置文件的設(shè)置技巧,需要的朋友可以參考下2016-04-04
VS2012實(shí)現(xiàn)簡(jiǎn)單登錄界面
這篇文章主要為大家詳細(xì)介紹了VS2012實(shí)現(xiàn)簡(jiǎn)單登錄界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
asp.net使用母版頁中使用ajax腳本取數(shù)據(jù)
因母版頁繼承自UserControl,我們無法像正常頁面那樣使用Jquey或Ajax的PageMethods等無刷新方法取數(shù)據(jù)。不過可以使用ajax提供的Sys.Net.WebRequest來解決這一問題。2010-09-09

