ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法
來源:易賢網(wǎng) 閱讀:3062 次 日期:2016-08-09 16:25:37
溫馨提示:易賢網(wǎng)小編為您整理了“ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法”,方便廣大網(wǎng)友查閱!

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();

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)