ASP.NET中DropDownList下拉框清單控制項綁定資料的4種方法_基礎應用

來源:互聯網
上載者:User

DropDownList Web 伺服器控制項使使用者能夠從預定義的列表中選擇一項。它與 ListBox Web 伺服器控制項的不同之處在於,其項列表在使用者單擊下拉按鈕之前一直處於隱藏狀態。另外,DropDownList 控制項與 ListBox 控制項的不同之處還在於它不支援多重選取模式。

DropDownList在html中的呈現對應的是select,下面讓我們來看一下DropDownList綁定資料的幾種方法。

一、把Array數組綁到DropDownList

複製代碼 代碼如下:

string[] Month =new string[7]{ "January", "February", "March", "April", "May", "June", "July" };
this.DropDownList1.DataSource = Month;
this.DropDownList1.DataBind();

這種方法只可以綁定一組資料到DropDownList,因為DropDownList可以綁定兩種資料:1是DataTextField、2是DataValueField,所以第一種方法綁定後DataTextField的值==DataTextField值。

二、把動態Array數組綁定到DropDownList

複製代碼 代碼如下:

ArrayList ar = new ArrayList();
for (int i = 1; i <=12; i++)
{
    ar.Add(i+"月");
}
this.DropDownList2.DataSource = ar;
this.DropDownList2.DataBind();

本質上就是講1到12月加到數組中,如下:

複製代碼 代碼如下:

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的方法,可以實現動態添加元素的功能,比方說,有一個DataTable,我們要把DataTable中一行的資料讀出來添加到Arraylist當中。

看我以下的示的代碼

複製代碼 代碼如下:

ArrayList ar = new ArrayList();
DataTable dt=dataset.Tables[0]
foreach (DataRow dr in dt.Rows)
{
    ar.Add(dr[0].ToString());
}

以上代碼從一個DataTable中通過foreach語句迴圈讀取Table中一行資料中第一個格的值添加到ArrayList當中。

三、將Hashtable綁定到Dropdownlist當中Hashtable的方法的好處是,它也可以綁定兩種資料一個是"key,一個是"value",這樣的話,我們就可以為dropdonwlist綁定上兩種不同的資料了。

複製代碼 代碼如下:

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對象綁定到dropdownlist

首先新增一個類,結構如下

複製代碼 代碼如下:

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;//匯入變數為屬性賦值
        MonthEN = en;//匯入變數為屬性賦值
       
    }
    public string MonthEN //構造屬性
    {
       get
        {
            return _MonthEN;
        }
        set
        {
            _MonthEN = value;
        }
    }
    public string MonthCN  //構造屬性
    {
        get
        {
            return _MonthCN;
        }
        set
        {
            _MonthCN = value;
        }
    }
}

Binder 方法

複製代碼 代碼如下:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.