How do you bind an Enum to a DropDownList control in ASP.NET?

后端 未结 25 2585
既然无缘
既然无缘 2020-11-29 15:41

Let\'s say I have the following simple enum:

enum Response
{
    Yes = 1,
    No = 2,
    Maybe = 3
}

How can I bind this enum to a DropDow

25条回答
  •  半阙折子戏
    2020-11-29 16:27

    This is my solution for Order an Enum and DataBind(Text and Value)to Dropdown using LINQ

    var mylist = Enum.GetValues(typeof(MyEnum)).Cast().ToList().OrderBy(l => l.ToString());
    foreach (MyEnum item in mylist)
        ddlDivisao.Items.Add(new ListItem(item.ToString(), ((int)item).ToString()));
    

提交回复
热议问题