下拉列表框DropDownList绑定Dictionary泛型类
2015-12-11 11:05
471 查看
DropDownList绑定Dictionary泛型类
定义一个Dictionary泛型类
/// <summary>
/// 产品类型
/// </summary>
/// <returns></returns>
public Dictionary<string, string> productType()
{
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("-1", "产品类型");
d.Add("1", "白酒");
d.Add("2", "啤酒");
d.Add("3", "葡萄酒");
d.Add("4", "保健酒");
d.Add("5", "洋酒");
d.Add("6", "黄果米酒");
return d;
}
DropDownList1绑定Dictionary泛型类作为数据源:
DropDownList1.DataSource = productType();
DropDownList1.DataTextField = "value";
DropDownList1.DataValueField = "key";
DropDownList1.DataBind();
Label1.Text = productType()["3"].ToString() + "__" + productType()["5"].ToString() ;//获取其中的某个值
查了一下网上基本都是这种简单的例子:
如果需要绑定一个对象:采用如下方式,
Dictionary<int,Customer> customerDic=由自定义的函数返回;
那么,dropdownlist控件数据源应该设置为:
customerDic.values,
其他两项设置需要绑定的对应的字段即可。
定义一个Dictionary泛型类
/// <summary>
/// 产品类型
/// </summary>
/// <returns></returns>
public Dictionary<string, string> productType()
{
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("-1", "产品类型");
d.Add("1", "白酒");
d.Add("2", "啤酒");
d.Add("3", "葡萄酒");
d.Add("4", "保健酒");
d.Add("5", "洋酒");
d.Add("6", "黄果米酒");
return d;
}
DropDownList1绑定Dictionary泛型类作为数据源:
DropDownList1.DataSource = productType();
DropDownList1.DataTextField = "value";
DropDownList1.DataValueField = "key";
DropDownList1.DataBind();
Label1.Text = productType()["3"].ToString() + "__" + productType()["5"].ToString() ;//获取其中的某个值
查了一下网上基本都是这种简单的例子:
如果需要绑定一个对象:采用如下方式,
Dictionary<int,Customer> customerDic=由自定义的函数返回;
那么,dropdownlist控件数据源应该设置为:
customerDic.values,
其他两项设置需要绑定的对应的字段即可。
相关文章推荐
- shell调用脚本--直接退出不执行后续命令
- centos7中mysql自带压测工具mysqlslap使用
- 搭建Hadoop集群中遇到的坑
- opencv 读取rgb.txt 并显示出来
- hadoop学习博客
- 5. storm常用命令(客户端storm shell)
- 编译opencv文件
- 拓扑排序(topological sort)
- hive shell执行命令报错:Specified key was too long; max key length is 767 bytes
- CentOS安装telnet
- CentOS6.5 配置IP的两种方式
- 常用网站背景颜色代码表
- 根据目前业务优化的新架构
- linux根分区扩容
- Linux内核开发之异步通知与异步I/O_fcntl
- class-dump 复制到/usr/bin目录不可写,Operation not permitted 解决办法
- linux-ssh免密码登录设置
- Open edXDevstack启动出现:could not connect to localhost:27017: [Errno 111] Connection refused.
- Project has no project.properties file! Edit the project properties
- linux dd 命令详解