ASP.NET中DropDownList下拉框列表控件绑定数据的4种方法

 更新时间:2016年04月19日 10:05:22   作者:Derek  
本文主要介绍了DropDownList控件4种绑定数据的基础用法,希望对大家能有所帮助。
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

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?CORE基础教程

    ASP.NET?CORE基础教程

    本文详细讲解了ASP.NET?CORE的基础教程,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • MVC中Action方法的返回类型介绍

    MVC中Action方法的返回类型介绍

    这篇文章介绍了MVC中的Action方法的返回类型,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • ASP.NET?Core基础之Main方法讲解

    ASP.NET?Core基础之Main方法讲解

    这篇文章介绍了ASP.NET?Core基础之Main方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-02-02
  • .Net使用XtraGrid控件绑定数据

    .Net使用XtraGrid控件绑定数据

    这篇文章介绍了.Net使用XtraGrid控件绑定数据的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • ASP.NET?Core中的Blazor组件介绍

    ASP.NET?Core中的Blazor组件介绍

    这篇文章介绍了ASP.NET?Core中的Blazor组件,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • ASP.NET中 ObjectDataSource控件的DataObjectTypeName属性

    ASP.NET中 ObjectDataSource控件的DataObjectTypeName属性

    本文主要介绍ObjectDataSource控件和DataObjectTypeName属性的用法,希望能给小伙伴们一些帮助。
    2016-04-04
  • .Net中控件的命名规则

    .Net中控件的命名规则

    这篇文章介绍了.Net中控件的命名规则,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • 初识 ASP.NET Membership 用户管理

    初识 ASP.NET Membership 用户管理

    Membership 是用做用户管理,进行身份认证使用的。通过ASP.NET Membership,我们可以创建用户、删除用户和编辑用户属性。所以这是一个实现登录相关控件的底层框架。
    2016-04-04
  • .Net行为型设计模式之迭代器模式(Iterator)

    .Net行为型设计模式之迭代器模式(Iterator)

    这篇文章介绍了.Net行为型设计模式之迭代器模式(Iterator),文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • ASP.NET?MVC项目部署方式介绍

    ASP.NET?MVC项目部署方式介绍

    这篇文章介绍了ASP.NET?MVC项目的部署方式,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-03-03

最新评论

?


http://www.vxiaotou.com