绑定Enum到DropDownList控件的方法
来源:互联网 发布:js e.target 获取属性 编辑:程序博客网 时间:2024/05/01 12:07
有时,需要将一个枚举类型绑定到DropDownList供用户选择,这主要是Enum.GetNames和Enum.GetValues的使用。
方法如下:
dropdownlist.DataSource = Enum.GetNames(typeof(YourEnumType));
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()));
}
- 绑定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到控件的方法
- PHP调用mplayer转换flv视频技巧
- javascript 统计字符串内某字符串出现次数
- JPGRAPH生成统计图(转自 翟鹏的博客)
- linux输入子系统(1)
- 短信数据结构
- 绑定Enum到DropDownList控件的方法
- QT程序无法输入中文的解决
- META http-equiv content 详解
- linux特殊权限和高级命令----宋轶聪
- mysql Linux导入数据
- 短信拦截
- PHP date()
- 对高内聚,低耦合的一点理解
- linux特殊权限和高级命令---宋轶聪