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

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

    You could use linq:

    var responseTypes= Enum.GetNames(typeof(Response)).Select(x => new { text = x, value = (int)Enum.Parse(typeof(Response), x) });
        DropDownList.DataSource = responseTypes;
        DropDownList.DataTextField = "text";
        DropDownList.DataValueField = "value";
        DropDownList.DataBind();
    

提交回复
热议问题