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

dropdownlist 绑定  时间:2021-02-08  阅读:()

★精品文档★

ASP.NET中DropDownList下拉框列表控件

绑定数据4种方法

DropDownList Web服务器控件使用户能够从预定义的列表中选择一项。它与ListBox Web服务器控件的不同之处在于其项列表在用户单击下拉按钮之前一直处于隐藏状态。另外DropDownList控件与ListBox控件的不同之处还在于它不支持多重选择模式。

DropDownList在html中的呈现对应的是select 下面让我们来看一下DropDownList绑定数据的几种方法。

一、把Array数组绑到DropDownList

代码如下:string[]Month=new string[7]{ “January”, “February”,“March”, “Apri l”, “May”, “June”, “July” };this.DropDownList1 .DataSource=Month;this.DropDownList1 .DataBind();

这种方法只可以绑定一组数据到DropDownList 因为DropDownList可以绑定两种数据 1是DataTextField、 2是DataValueField 所以第一种方法绑定后DataTextField的值==DataTextField值。

二、把动态Array数组绑定到DropDownList

代码如下:

ArrayList ar=new ArrayList();

1/7

★精品文档★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中一行的数据读出来添加到Arrayl ist当中。

看我以下的示的代码

代码如下:

ArrayList ar=new ArrayList();

2/7

★精品文档★

DataTable dt=dataset.Tables[0]foreach (DataRow dr in dt.Rows)

{

  ar.Add(dr[0].ToString());

}

以上代码从一个DataTable中通过foreach语句循环读取Table中一行数据中第一个格的值添加到ArrayList当中。

三、将Hashtable绑定到Dropdownl ist当中Hashtable的方法的好处是它也可以绑定两种数据一个是”key,一个是”value”,这样的话我们就可以为dropdonwl ist绑定上两种不同的数据了。

代码如下:

Hashtable Ht=new Hashtable();

Ht.Add(“January”, “1月”);

Ht.Ad d(“Fe bru a ry”, “2月”);

Ht.Ad d(“M a rch”, “3月”);

Ht.Add(“Apri l”, “4月”);

Ht.Add(“May”, “5月”);

Ht.Add(“June”, “6月”);

Ht.Add(“July”, “7月”);this.DropDownList3.DataSource=Ht;this.DropDownList3.DataValueField=“key”;

3/7

★精品文档★this.DropDownList3.DataTextField=“value”;this.DropDownList3.DataBind();

四、把Object对象绑定到dropdownl ist

首先新增一个类结构如下

代码如下:publ ic class ClassMonth

{

    private string _MonthEN =DateTime.Now.ToString(“MMMM”,System.Global ization.Cu ltureInfo.CreateSpecificCu lture(“en”));

    private string _MonthCN =DateTime.Now.ToString(“MMMM”,

System.Global ization.CultureInfo.CreateSpecificCulture(“zh-CN”));

  publ ic ClassMonth()

   {

        MonthCN =DateTime.Now.ToString(“MMMM”,

System.Global ization.CultureInfo.CreateSpecificCulture(“zh-CN”));

        MonthEN =DateTime.Now.ToString(“MMMM”,

4/7

★精品文档★

System.Global ization.CultureInfo.CreateSpecificCulture(“en”));

   }

  publ ic ClassMonth(string cn,string en)

   {

    MonthCN=cn;//导入变量为属性赋值

    MonthEN=en;//导入变量为属性赋值

   }

  publ ic string MonthEN//构造属性

   {

    get

     {

            return_MonthEN;

     }

    set

     {

           _MonthEN=value;

     }

5/7

★精品文档★

   }

   publ ic string MonthCN //构造属性

   {

    get

     {

            return_MonthCN;

     }

    set

     {

           _MonthCN=value;

     }

   }

}

绑定方法

代码如下:

ArrayList arl ist=new ArrayList();arl ist.Add(new ClassMonth(“1月”, “January”));arl ist.Add(new ClassMonth(“2月”, “February”));arl ist.Add(new ClassMonth(“3月”, “March”));

6/7

★精品文档★arl ist.Add(new ClassMonth(“4月”, “Apri l”));arl ist.Add(new ClassMonth(“5月”, “May”));this.DropDownList4.DataSource=arl ist;this.DropDownList4.DataValueField=“MonthEN”;this.DropDownList4.DataTextField=“MonthCN”;this.DropDownList4.DataBind();

7/7

hypervmart:英国/荷兰vps,2核/3GB内存/25GB NVMe空间/不限流量/1Gbps端口/Hyper-V,$10.97/季

hypervmart怎么样?hypervmart是一家国外主机商,成立于2011年,提供虚拟主机、VPS等,vps基于Hyper-V 2012 R2,宣称不超售,支持linux和windows,有荷兰和英国2个数据中心,特色是1Gbps带宽、不限流量。现在配置提高,价格不变,性价比提高了很多。(数据中心不太清楚,按以前的记录,应该是欧洲),支持Paypal付款。点击进入:hypervmart官方网...

GreenCloudVPS($30/年),500G大硬盘VPS,10Gbps带宽

GreenCloudVPS最近在新加坡DC2节点上了新机器,Dual Xeon Silver 4216 CPU,DDR4内存,10Gbps网络端口,推出了几款大硬盘VPS套餐,基于KVM架构,500GB磁盘起年付30美元。除了大硬盘套餐外,还加推了几款采用NVMe硬盘的常规套餐,最低年付20美元。不过需要提醒的是,机房非直连中国,尤其是电信用户ping值感人,包括新加坡DC1也是如此。大硬盘VPS...

OneTechCloud香港/日本/美国CN2 GIA月付9折季付8折,可选原生IP或高防VPS

OneTechCloud(易科云)是一家主打CN2等高端线路的VPS主机商家,成立于2019年,提供的产品包括VPS主机和独立服务器租用等,数据中心可选美国洛杉矶、中国香港、日本等,有CN2 GIA线路、AS9929、高防、原生IP等。目前商家针对全场VPS主机提供月付9折,季付8折优惠码,优惠后香港VPS最低季付64元起(≈21.3元/月),美国洛杉矶CN2 GIA线路+20Gbps防御型VPS...

dropdownlist 绑定为你推荐
在线代理网站求有效的代理服务器地址?彩信中心移动的彩信中心是?主页是?收不到彩信,怎么设置?怎么点亮qq空间图标怎么点亮QQ空间的图标ejb开发EJB是什么?系统分析员系统分析师是什么职业?主要做什么工作?网络虚拟机VMware虚拟机三种网络模式的区别有哪些?服务器连接异常手机服务器连接异常网站地图制作给人看的那种,网站地图怎么做去鼠标加速度请教如何正确的去处鼠标加速度~263企业邮箱设置263企业邮箱如何添加新的信箱?
怎样注册域名 安徽双线服务器租用 免费linux主机 免费域名跳转 windows2003iso 免费全能主机 南通服务器 中国网通测速 东莞服务器 四核服务器 raid10 双12 根服务器 移动服务器托管 西安服务器托管 电信主机托管 塔式服务器 windows2008 winserver2008r2 标准机柜 更多