绑定Enum到DropDownList控件的方法
来源:互联网 发布:知乎91改名tossboy怎么 编辑:程序博客网 时间:2024/05/01 15:21
有时,需要将一个枚举类型绑定到DropDownList供用户选择,这主要是Enum.GetNames和Enum.GetValues的使用。
方法如下:
dropdownlist.DataSource = Enum.GetNames(typeof(YourEnumType));
dropdownlist.DataBind();
dropdownlist.DataBind();
这样,可以直接将Enum绑定到DropDownList控件上,不过有个缺点是,DropDownList的value也是枚举的名称而不是枚举代表的数值,有时,我是需要枚举数值的,因为一般以数值方式存储到数据库。这时可以使用下面的方法:
string[] names = Enum.GetNames(typeof(YourEnumType));
int[] values = (int[])Enum.GetValues(typeof(YourEnumType));
for (int i = 0; i < names.Length; i++)
{
dropdownlist.Items.Add(new ListItem(names[i], values[i].ToString()));
}
int[] values = (int[])Enum.GetValues(typeof(YourEnumType));
for (int i = 0; i < names.Length; i++)
{
dropdownlist.Items.Add(new ListItem(names[i], values[i].ToString()));
}
中文的时候你可以这样写:
public enum A
{
壹,
贰,
叁
}
{
壹,
贰,
叁
}
你甚至可以这样写:
public enum 我的枚举
{
壹,
贰,
叁
}
{
壹,
贰,
叁
}
- 绑定Enum到DropDownList控件的方法
- 绑定Enum到DropDownList控件的方法
- [摘录]绑定Enum到DropDownList控件的方法
- Enum绑定DropDownList方法(转)
- DropDownList 绑定 枚举 Enum
- -将DropDownList控件绑定 到GridView控当中,并调用方法给DropDownList控件在页面进行数据绑定
- DropDownList控件的数据绑定
- DropDownList控件的数据绑定
- DropDownList绑定数据源的方法
- DropDownList绑定数据源的方法
- 关于DropDownList绑定到数据库中的方法
- 两种添加数据到WEB DropDownList 控件的方法
- 绑定控件数据源对于DropDownList类型的
- DropDownList控件的使用(数据绑定)
- 数据库信息绑定DropDownList控件的Item
- DropDownList控件的使用(数据绑定)
- DropDownList控件的数据绑定方式
- WPF 绑定StaticResource到控件的方法
- Linux下C语言编程--文件的操作
- 我的迷茫
- 思
- 大学知识技能的就业职位招聘情况
- 写在博客开篇之际
- 绑定Enum到DropDownList控件的方法
- 程序员如何追女孩---(声明这是转载,本人是男的!)
- 基本数据结构的python实现---堆栈
- Linux信号量 共享内存和消息队列
- shell脚本和命令调用
- Linux与Windows下可变参数函数问题
- JAVA中的位移操作 >> >>
- 安装wordpress的Warning: Cannot modify header information 问题
- 最长上升子序列LIS算法实现