How to Bind Enum Types to the DropDownList?

后端 未结 6 1200
终归单人心
终归单人心 2020-12-24 11:29

If I have the following enum

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

and I have DropDownList

6条回答
  •  难免孤独
    2020-12-24 12:25

    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);
    

提交回复
热议问题