★精品文档★
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
昨天有分享到"2021年Vultr新用户福利注册账户赠送50美元"文章,居然还有网友曾经没有注册过他家的账户,薅过他们家的羊毛。通过一阵折腾居然能注册到账户,但是对于如何开通云服务器稍微有点不对劲,对于新人来说确实有点疑惑。因为Vultr采用的是预付费充值方式,会在每月的一号扣费,当然我们账户需要存留余额或者我们采用自动扣费支付模式。把笔记中以前的文章推送给网友查看,他居然告诉我界面不同,看的不对...
触摸云触摸云(cmzi.com),国人商家,有IDC/ISP正规资质,主营香港线路VPS、物理机等产品。本次为大家带上的是美国高防2区的套餐。去程普通线路,回程cn2 gia,均衡防御速度与防御,防御值为200G,无视UDP攻击,可选择性是否开启CC防御策略,超过峰值黑洞1-2小时。最低套餐20M起,多数套餐为50M,适合有防御型建站需求使用。美国高防2区 弹性云[大宽带]· 配置:1-16核· ...
ttcloud怎么样?ttcloud是一家海外服务器厂商,运营服务器已经有10年时间,公司注册地址在香港地区,业务范围包括服务器托管,机柜托管,独立服务器等在内的多种服务。我们后台工单支持英文和中文服务。TTcloud最近推出了新上架的日本独立服务器促销活动,价格 $70/月起,季付送10Mbps带宽。也可以跟进客户的需求进行各种DIY定制。点击进入:ttcloud官方网站地址TTcloud拥有自...