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

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

    The accepted solution doesn't work, but the code below will help others looking for the shortest solution.

     foreach (string value in Enum.GetNames(typeof(Response)))
                        ddlResponse.Items.Add(new ListItem()
                        {
                            Text = value,
                            Value = ((int)Enum.Parse(typeof(Response), value)).ToString()
                        });
    

提交回复
热议问题