4种方法把数据绑定到DropDownList
来源:互联网 发布:apache和nginx用哪个 编辑:程序博客网 时间:2024/05/14 09:35
第一种,把Array数组绑到dropdownlist
string[] Month =new string[7]{ "January", "February", "March", "April", "May", "June", "July" }; this.DropDownList1.DataSource = Month; this.DropDownList1.DataBind();
第二种,把Array数组绑定到dropdownlist
ArrayList ar = new ArrayList(); for (int i = 1; i <= 12; i++) { ar.Add(i + "月"); } this.DropDownList2.DataSource = ar; this.DropDownList2.DataBind();
第2种方法的好处是通过ArrayList.Add的方法,可以实现动态添加元素的功能,比方说,有一个DataTable,我们要把DataTable中一行的数据读出来添加到Arraylist当中。
看我以下的示的代码
ArrayList ar = new ArrayList(); DataTable dt=dataset.Tables[0] foreach (DataRow dr in dt.Rows) { ar.Add(dr[0].ToString()); }
以上代码从一个DataTable中通过foreach语句循环读取Table中一行数据中第一个格的值添加到ArrayList当中。
第三种方法,将Hashtable绑定到Dropdownlist当中Hashtable的方法的好处是,它也可以绑定两种数据一个是"key,一个是"value",这样的话,我们就可以为dropdonwlist绑定上两种不同的数据了。
Hashtable Ht = new Hashtable(); Ht.Add("January", "1月"); Ht.Add("February", "2月"); Ht.Add("March", "3月"); Ht.Add("April", "4月"); Ht.Add("May", "5月"); Ht.Add("June", "6月"); Ht.Add("July", "7月"); this.DropDownList3.DataSource = Ht; this.DropDownList3.DataValueField = "key"; this.DropDownList3.DataTextField = "value"; this.DropDownList3.DataBind();
第四种,把Object对象绑定到dropdownlist
首先新增一个类,结构如下
public class ClassMonth{ private string _MonthEN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("en")); private string _MonthCN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN")); public ClassMonth() { MonthCN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN")); MonthEN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("en")); } public ClassMonth(string cn, string en) { MonthCN = cn;//导入变量为属性赋值 MonthEN = en;//导入变量为属性赋值 } public string MonthEN //构造属性 { get { return _MonthEN; } set { _MonthEN = value; } } public string MonthCN //构造属性 { get { return _MonthCN; } set { _MonthCN = value; } }}
绑定方法:
ArrayList arlist = new ArrayList(); arlist.Add(new ClassMonth("1月", "January")); arlist.Add(new ClassMonth("2月", "February")); arlist.Add(new ClassMonth("3月", "March")); arlist.Add(new ClassMonth("4月", "April")); arlist.Add(new ClassMonth("5月", "May")); this.DropDownList4.DataSource = arlist; this.DropDownList4.DataValueField = "MonthEN"; this.DropDownList4.DataTextField = "MonthCN"; this.DropDownList4.DataBind();
转载于:http://www.2cto.com/os/201504/395460.html
0 0
- 4种方法把数据绑定到Dropdownlist
- [Asp.Net]4种方法把数据绑定到Dropdownlist
- 4种方法把数据绑定到DropDownList
- DropDownList绑定数据的两种方法
- -将DropDownList控件绑定 到GridView控当中,并调用方法给DropDownList控件在页面进行数据绑定
- XML文件读取数据绑定到DropDownList
- 绑定Enum到DropDownList控件的方法
- 关于DropDownList绑定到数据库中的方法
- 绑定Enum到DropDownList控件的方法
- DropDownList与DataTable数据绑定的方法
- 枚举绑定到DropDownList
- 绑定到 DropDownList
- 两种添加数据到WEB DropDownList 控件的方法
- DropDownList 的数据绑定
- dropdownlist 动态绑定数据
- DropDownList数据绑定
- DropDownList绑定数据
- dropdownlist 数据绑定问题
- Android 开源交流分享汇总
- 微信公众号与传统企业对接
- 红尘小女子
- CheckBox点击刷新后是否乱序
- An internal error occurred during: "Validating **".问题处理
- 4种方法把数据绑定到DropDownList
- Boost使用Thread库
- python 机器学习——文本特征抽取与向量化
- 安卓多线程处理(转)
- 最小优先级队列
- PHP开发人员容易忽略的几点精华
- 全栈工程师有什么用(转载)
- mybites 大于 小于符号的情况
- Oracle之分析函数、开窗函数