★精品文档★
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
俄罗斯vps云服务器商家推荐!俄罗斯VPS,也叫毛子主机(毛子vps),因为俄罗斯离中国大陆比较近,所以俄罗斯VPS的延迟会比较低,国内用户也不少,例如新西伯利亚机房和莫斯科机房都是比较热门的俄罗斯机房。这里为大家整理推荐一些好用的俄罗斯VPS云服务器,这里主要推荐这三家:justhost、ruvds、justg等俄罗斯vps主机,方便大家对比购买适合自己的俄罗斯VPS。一、俄罗斯VPS介绍俄罗斯...
数脉科技怎么样?昨天看到数脉科技发布了7月优惠,如果你想购买香港服务器,可以看看他家的产品,性价比还是非常高的。数脉科技对香港自营机房的香港服务器进行超低价促销,可选择10M、30M的优质bgp网络。目前商家有优质BGP、CN2、阿里云线路,国内用户用来做站非常不错,目前E3/16GB阿里云CN2线路的套餐有一个立减400元的优惠,有需要的朋友可以看看。点击进入:数脉科技商家官方网站香港特价阿里云...
物语云计算(MonogatariCloud)是一家成立于2016年的老牌国人商家,主营国内游戏高防独服业务,拥有多家机房资源,产品质量过硬,颇有一定口碑。本次带来的是美国圣何塞 Equinix 机房的高性能I9-10980XE大带宽VPS,去程CN2GIA回程AS9929,美国原生IP,支持解锁奈飞等应用,支持免费安装Windows系统。值得注意的是,物语云采用的虚拟化技术为Hyper-V,资源全...