溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的方法有哪些

發(fā)布時(shí)間:2021-09-16 16:33:11 來(lái)源:億速云 閱讀:413 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要為大家展示了“ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的方法有哪些”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的方法有哪些”這篇文章吧。

DropDownList Web 服務(wù)器控件使用戶能夠從預(yù)定義的列表中選擇一項(xiàng)。它與 ListBox Web 服務(wù)器控件的不同之處在于,其項(xiàng)列表在用戶單擊下拉按鈕之前一直處于隱藏狀態(tài)。另外,DropDownList 控件與 ListBox 控件的不同之處還在于它不支持多重選擇模式。

DropDownList在html中的呈現(xiàn)對(duì)應(yīng)的是select,下面讓我們來(lái)看一下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();

這種方法的好處是通過(guò)ArrayList.Add的方法,可以實(shí)現(xiàn)動(dòng)態(tài)添加元素的功能,比方說(shuō),有一個(gè)DataTable,我們要把DataTable中一行的數(shù)據(jù)讀出來(lái)添加到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中通過(guò)foreach語(yǔ)句循環(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中DropDownList下拉框列表控件綁定數(shù)據(jù)的方法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI