★精品文档★
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
物语云计算怎么样?物语云计算(MonogatariCloud)是一家成立于2016年的老牌国人商家,主营国内游戏高防独服业务,拥有多家机房资源,产品质量过硬,颇有一定口碑。本次带来的是特惠活动为美国洛杉矶Cera机房的不限流量大带宽VPS,去程直连回程4837,支持免费安装Windows系统。值得注意的是,物语云采用的虚拟化技术为Hyper-v,因此并不会超售超开。一、物语云官网点击此处进入物语云...
RAKSmart 商家最近动作还是比较大的,比如他们也在增加云服务器产品,目前已经包含美国圣何塞和洛杉矶机房,以及这个月有新增的中国香港机房,根据大趋势云服务器算是比较技术流的趋势。传统的VPS主机架构方案在技术层面上稍微落后一些,当然也是可以用的。不清楚是商家出于对于传统VPS主机清理库存,还是多渠道的产品化营销,看到RAKSmart VPS主机提供美国、香港和日本机房的半价促销,当然也包括其他...
阿里云(aliyun)在这个月又推出了一个金秋上云季活动,到9月30日前,每天两场秒杀活动,包括轻量应用服务器、云服务器、云数据库、短信包、存储包、CDN流量包等等产品,其中Aliyun轻量云服务器最低60元/年起,还可以99元续费3次!活动针对新用户和没有购买过他们的产品的老用户均可参与,每人限购1件。关于阿里云不用多说了,国内首屈一指的云服务器商家,无论建站还是学习都是相当靠谱的。活动地址:h...