欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2016年04月19日 10:05:22   作者:Derek  
本文主要介紹了DropDownList控件4種綁定數(shù)據(jù)的基礎(chǔ)用法,希望對(duì)大家能有所幫助。

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

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

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ù)組中,如下:

復(fù)制代碼 代碼如下:

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)中。

看我以下的示的代碼

復(fù)制代碼 代碼如下:

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ù)了。

復(fù)制代碼 代碼如下:

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)如下

復(fù)制代碼 代碼如下:

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

綁定方法

復(fù)制代碼 代碼如下:

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

相關(guān)文章

  • ASP.NET?MVC框架簡(jiǎn)介

    ASP.NET?MVC框架簡(jiǎn)介

    這篇文章介紹了ASP.NET?MVC框架,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • C#命名空間System.ComponentModel屬性方法匯總

    C#命名空間System.ComponentModel屬性方法匯總

    本文詳細(xì)講解了C#命名空間System.ComponentModel屬性方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • ASP.NET?MVC模式中應(yīng)用程序結(jié)構(gòu)詳解

    ASP.NET?MVC模式中應(yīng)用程序結(jié)構(gòu)詳解

    本文詳細(xì)講解了ASP.NET?MVC模式中的應(yīng)用程序結(jié)構(gòu),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • .Net行為型設(shè)計(jì)模式之備忘錄模式(Memento)

    .Net行為型設(shè)計(jì)模式之備忘錄模式(Memento)

    這篇文章介紹了.Net行為型設(shè)計(jì)模式之備忘錄模式(Memento),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • .NET4.0版本中基于任務(wù)的異步模式(TAP)

    .NET4.0版本中基于任務(wù)的異步模式(TAP)

    這篇文章介紹了.NET4.0版本中基于任務(wù)的異步模式(TAP),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • ASP.NET中使用用戶控件

    ASP.NET中使用用戶控件

    這篇文章介紹了ASP.NET中使用用戶控件的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • ASP.NET Core服務(wù)生命周期

    ASP.NET Core服務(wù)生命周期

    這篇文章介紹了ASP.NET Core中的服務(wù)生命周期,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • Blazor路由與頁面導(dǎo)航開發(fā)介紹

    Blazor路由與頁面導(dǎo)航開發(fā)介紹

    這篇文章介紹了Blazor路由與頁面導(dǎo)航開發(fā),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • ASP.NET與數(shù)據(jù)庫相關(guān)技巧

    ASP.NET與數(shù)據(jù)庫相關(guān)技巧

    [紅色]ASP.NET與數(shù)據(jù)庫相關(guān)技巧...
    2006-10-10
  • 詳解Asp.net 5中的ApplicationBuilder

    詳解Asp.net 5中的ApplicationBuilder

    這篇文章介紹了Asp.net 5中的ApplicationBuilder,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01

最新評(píng)論