国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net中ListBox 綁定多個選項為選中及刪除實現(xiàn)方法

2024-07-10 13:24:09
字體:
來源:轉載
供稿:網(wǎng)友
我們先來看listbox綁定多選項實現(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 runat="server">
<asp:ListBox 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 綁定多個選項為選中及刪除實現(xiàn)方法



.aspx:

復制代碼 代碼如下:


<asp:TextBox runat="server"></asp:TextBox>
<br />
<asp:Button runat="server" Text="Binding" />
<br />
<br />
<asp:ListBox runat="server" SelectionMode="Multiple" ></asp:ListBox> .


aspx.cs中,首先是為ListBox準備數(shù)據(jù),然后對ListBox控件進行數(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", );
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", );
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();
}


一網(wǎng)友回復
foreach( object d in listBox2.SelectedItems)
這一行有問題,你知道,當你刪除其中一個選項時,listBOx的所選項已經(jīng)被更改了,你再調(diào)用foreach,當然會有問題!
解決方法是將listBox2.SelectedItems先放入一個數(shù)組變量再行調(diào)用就沒問題了!
不過當然更簡單的方法就是直接調(diào)用
listBox2.ClearSelected();
是的,這一句話就解決了所有的問題!所有的被選擇項目都會被清除掉
總結
本文章講述了listbox從綁定多個選項和listbox綁定多個選項的同時設置多個選中狀態(tài)的選項,到最后如何刪除多個己綁定的選項方法。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 临颍县| 荆州市| 朔州市| 郯城县| 自贡市| 襄汾县| 神木县| 泸西县| 富裕县| 开远市| 澄江县| 讷河市| 张家川| 洛南县| 满城县| 忻州市| 阿勒泰市| 塔河县| 邯郸县| 汉阴县| 蕉岭县| 饶河县| 云龙县| 甘肃省| 翁牛特旗| 哈尔滨市| 涟源市| 青海省| 海伦市| 定西市| 寻甸| 紫阳县| 楚雄市| 元氏县| 黄大仙区| 嘉荫县| 赫章县| 调兵山市| 温泉县| 穆棱市| 凌海市|