如何将枚举类型绑定到DropDownList? [英] How to Bind Enum Types to the DropDownList?

查看:116
本文介绍了如何将枚举类型绑定到DropDownList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下枚举

public enum EmployeeType
{
    Manager = 1,
    TeamLeader,
    Senior,
    Junior
}

我有DropDownList,我想绑定这个 EmployeeType 枚举到DropDownList,有没有办法这样做?

and I have DropDownList and I want to bind this EmployeeType enum to the DropDownList, is there any way to do that?

推荐答案

如果你有DropDownList对象,叫做ddl,你可以这样做,如下

if you have DropDownList object called ddl you can do it as below

ddl.DataSource = Enum.GetNames(typeof(EmployeeType));
ddl.DataBind();

如果您想要枚举值返回选择....

if you want the Enum value Back on Selection ....

 EmployeeType empType = (EmployeeType)Enum.Parse(typeof(EmployeeType), ddl.SelectedValue);

这篇关于如何将枚举类型绑定到DropDownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆