ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法
DropDownList Web 服務(wù)器控件使用戶能夠從預(yù)定義的列表中選擇一項(xiàng)。它與 ListBox Web 服務(wù)器控件的不同之處在于,其項(xiàng)列表在用戶單擊下拉按鈕之前一直處于隱藏狀態(tài)。另外,DropDownList 控件與 ListBox 控件的不同之處還在于它不支持多重選擇模式。
DropDownList在html中的呈現(xiàn)對(duì)應(yīng)的是select,下面讓我們來看一下DropDownList綁定數(shù)據(jù)的幾種方法。
一、把Array數(shù)組綁到DropDownList
string[] Month =new string[7]{ "January", "February", "March", "April", "May", "June", "July" };
this.DropDownList1.DataSource = Month;
this.DropDownList1.DataBind();
這種方法只可以綁定一組數(shù)據(jù)到DropDownList,因?yàn)镈ropDownList可以綁定兩種數(shù)據(jù):1是DataTextField、2是DataValueField,所以第一種方法綁定后DataTextField的值==DataTextField值。
二、把動(dòng)態(tài)Array數(shù)組綁定到DropDownList
ArrayList ar = new ArrayList();
for (int i = 1; i <=12; i++)
{
ar.Add(i+"月");
}
this.DropDownList2.DataSource = ar;
this.DropDownList2.DataBind();
本質(zhì)上就是講1到12月加到數(shù)組中,如下:
ArrayList ar = new ArrayList();
ar.Add("1月");
ar.Add("2月");
ar.Add("3月");
ar.Add("4月");
...
this.DropDownList2.DataSource = ar;
this.DropDownList2.DataBind();
這種方法的好處是通過ArrayList.Add的方法,可以實(shí)現(xiàn)動(dòng)態(tài)添加元素的功能,比方說,有一個(gè)DataTable,我們要把DataTable中一行的數(shù)據(jù)讀出來添加到Arraylist當(dāng)中。
看我以下的示的代碼
ArrayList ar = new ArrayList();
DataTable dt=dataset.Tables[0]
foreach (DataRow dr in dt.Rows)
{
ar.Add(dr[0].ToString());
}
以上代碼從一個(gè)DataTable中通過foreach語句循環(huán)讀取Table中一行數(shù)據(jù)中第一個(gè)格的值添加到ArrayList當(dāng)中。
三、將Hashtable綁定到Dropdownlist當(dāng)中Hashtable的方法的好處是,它也可以綁定兩種數(shù)據(jù)一個(gè)是"key,一個(gè)是"value",這樣的話,我們就可以為dropdonwlist綁定上兩種不同的數(shù)據(jù)了。
Hashtable Ht = new Hashtable();
Ht.Add("January", "1月");
Ht.Add("February", "2月");
Ht.Add("March", "3月");
Ht.Add("April", "4月");
Ht.Add("May", "5月");
Ht.Add("June", "6月");
Ht.Add("July", "7月");
this.DropDownList3.DataSource = Ht;
this.DropDownList3.DataValueField = "key";
this.DropDownList3.DataTextField = "value";
this.DropDownList3.DataBind();
四、把Object對(duì)象綁定到dropdownlist
首先新增一個(gè)類,結(jié)構(gòu)如下
public class ClassMonth
{
private string _MonthEN = DateTime.Now.ToString("MMMM",System.Globalization.CultureInfo.CreateSpecificCulture("en"));
private string _MonthCN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN"));
public ClassMonth()
{
MonthCN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN"));
MonthEN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("en"));
}
public ClassMonth(string cn,string en)
{
MonthCN = cn;//導(dǎo)入變量為屬性賦值
MonthEN = en;//導(dǎo)入變量為屬性賦值
}
public string MonthEN //構(gòu)造屬性
{
get
{
return _MonthEN;
}
set
{
_MonthEN = value;
}
}
public string MonthCN //構(gòu)造屬性
{
get
{
return _MonthCN;
}
set
{
_MonthCN = value;
}
}
}
綁定方法
ArrayList arlist=new ArrayList();
arlist.Add(new ClassMonth("1月", "January"));
arlist.Add(new ClassMonth("2月", "February"));
arlist.Add(new ClassMonth("3月", "March"));
arlist.Add(new ClassMonth("4月", "April"));
arlist.Add(new ClassMonth("5月", "May"));
this.DropDownList4.DataSource = arlist;
this.DropDownList4.DataValueField = "MonthEN";
this.DropDownList4.DataTextField = "MonthCN";
this.DropDownList4.DataBind();
- ASP.NET?MVC實(shí)現(xiàn)多選下拉框
- 詳解ASP.NET MVC 下拉框的傳值的兩種方式
- ASP .NET 可編輯輸入自動(dòng)匹配的下拉框
- 詳解ASP.NET MVC之下拉框綁定四種方式
- ASP.NET MVC下拉框聯(lián)動(dòng)實(shí)例解析
- ASP.NET實(shí)現(xiàn)級(jí)聯(lián)下拉框效果實(shí)例講解
- ASP.NET多彩下拉框開發(fā)實(shí)例
- asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- asp.net中js+jquery添加下拉框值和后臺(tái)獲取示例
- asp.net 實(shí)現(xiàn)下拉框只讀功能
- ASP.NET?MVC下拉框中顯示枚舉項(xiàng)
相關(guān)文章
C#命名空間System.ComponentModel屬性方法匯總
本文詳細(xì)講解了C#命名空間System.ComponentModel屬性方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01ASP.NET?MVC模式中應(yīng)用程序結(jié)構(gòu)詳解
本文詳細(xì)講解了ASP.NET?MVC模式中的應(yīng)用程序結(jié)構(gòu),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03.Net行為型設(shè)計(jì)模式之備忘錄模式(Memento)
這篇文章介紹了.Net行為型設(shè)計(jì)模式之備忘錄模式(Memento),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05ASP.NET與數(shù)據(jù)庫相關(guān)技巧
[紅色]ASP.NET與數(shù)據(jù)庫相關(guān)技巧...2006-10-10詳解Asp.net 5中的ApplicationBuilder
這篇文章介紹了Asp.net 5中的ApplicationBuilder,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01