★精品文档★
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
Virtono是一家成立于2014年的国外VPS主机商,提供VPS和服务器租用等产品,商家支持PayPal、信用卡、支付宝等国内外付款方式,可选数据中心共7个:罗马尼亚2个,美国3个(圣何塞、达拉斯、迈阿密),英国和德国各1个。目前,商家针对美国圣何塞机房VPS提供75折优惠码,同时,下单后在LET回复订单号还能获得双倍内存的升级。下面以圣何塞为例,分享几款VPS主机配置信息。Cloud VPSC...
UCloud优刻得近日针对全球大促活动进行了一次改版,这次改版更加优惠了,要比之前的优惠价格还要低一些,并且新增了1核心1G内存的快杰云服务器,2元/首年,47元/年,这个价格应该是目前市面上最低最便宜的云服务器产品了,有需要国内外便宜VPS云服务器的朋友可以关注一下。UCloud好不好,UCloud服务器怎么样?UCloud服务器值不值得购买UCloud是优刻得科技股份有限公司旗下拥有的云计算服...
最近发现一个比较怪异的事情,在访问和登录大部分国外主机商和域名商的时候都需要二次验证。常见的就是需要我们勾选判断是不是真人。以及比如在刚才要访问Namecheap检查前几天送给网友域名的账户域名是否转出的,再次登录网站的时候又需要人机验证。这里有看到"Attention Required"的提示。我们只能手工选择按钮,然后根据验证码进行选择合适的标记。这次我要选择的是船的标识,每次需要选择三个,一...