★精品文档★
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
炭云怎么样?炭云(之前的碳云),国人商家,正规公司(哈尔滨桓林信息技术有限公司),主机之家测评介绍过多次。现在上海CN2共享IP的VPS有一款特价,上海cn2 vps,2核/384MB内存/8GB空间/800GB流量/77Mbps端口/共享IP/Hyper-v,188元/年,特别适合电信网络。有需要的可以关注一下。点击进入:炭云官方网站地址炭云vps套餐:套餐cpu内存硬盘流量/带宽ip价格购买上...
湖南百纵科技有限公司是一家具有ISP ICP 电信增值许可证的正规公司,多年不断转型探索现已颇具规模,公司成立于2009年 通过多年经营积累目前已独具一格,公司主要经营有国内高防服务器,香港服务器,美国服务器,站群服务器,东南亚服务器租用,国内香港美国云服务器,以及全球专线业务!活动方案:主营:1、美国CN2云服务器,美国VPS,美国高防云主机,美国独立服务器,美国站群服务器,美国母机。2、香港C...
HostKvm商家我们也不用多介绍,这个服务商来自国内某商家,旗下也有多个品牌的,每次看到推送信息都是几个服务商品牌一起推送的。当然商家还是比较稳定的,商家品牌比较多,这也是国内商家一贯的做法,这样广撒网。这次看到黑五优惠活动发布了,针对其主打的香港云服务器提供终身6折的优惠,其余机房服务器依然是8折,另还有充值50美元赠送5美元的优惠活动,有需要的可以看看。HostKvm是一个创建于2013年的...