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

字號(hào):


    DropDownList Web 服務(wù)器控件使用戶(hù)能夠從預(yù)定義的列表中選擇一項(xiàng)。它與 ListBox Web 服務(wù)器控件的不同之處在于,其項(xiàng)列表在用戶(hù)單擊下拉按鈕之前一直處于隱藏狀態(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è)類(lèi),結(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();