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

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

    After finding this answer I came up with what I think is a better (at least more elegant) way of doing this, thought I'd come back and share it here.

    Page_Load:

    DropDownList1.DataSource = Enum.GetValues(typeof(Response));
    DropDownList1.DataBind();
    

    LoadValues:

    Response rIn = Response.Maybe;
    DropDownList1.Text = rIn.ToString();
    

    SaveValues:

    Response rOut = (Response) Enum.Parse(typeof(Response), DropDownList1.Text);
    

提交回复
热议问题