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

后端 未结 25 2590
既然无缘
既然无缘 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:28

    My version is just a compressed form of the above:

    foreach (Response r in Enum.GetValues(typeof(Response)))
    {
        ListItem item = new ListItem(Enum.GetName(typeof(Response), r), r.ToString());
        DropDownList1.Items.Add(item);
    }
    

提交回复
热议问题