国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:30:14
字體:
來源:轉載
供稿:網友
本文主要介紹了DropDownList控件4種綁定數據的基礎用法,希望對大家能有所幫助。
 

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;
        }
    }
}

 

綁定方法

 

復制代碼代碼如下:

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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郸城县| 平顶山市| 方城县| 巴塘县| 武安市| 邢台县| 楚雄市| 南部县| 拉萨市| 通城县| 青冈县| 比如县| 福安市| 辉南县| 铜梁县| 尉犁县| 咸阳市| 津南区| 遵化市| 鹤岗市| 黎川县| 晋江市| 长岛县| 潮州市| 湖北省| 永寿县| 洪洞县| 隆回县| 上蔡县| 宁乡县| 勐海县| 河池市| 乌兰察布市| 北票市| 溧阳市| 海安县| 长乐市| 若羌县| 无极县| 远安县| 南充市|