Thursday, February 1, 2007

Enum and DropDowns

So I need to get the name and value of an Enum to tie sensibly to a DropDownList. This is how you do it:

Enum definition:

    public enum Categories
    {
        Wildlife,
        Resorts,
        Beer,
        Other
    }



To tie to DropDownList called ddCategory:

    ddCategory.DataSource = Enum.GetNames(typeof(Categories));
    ddCategory.DataBind();

To set the selected value:


    ddCategory.SelectedValue = Enum.GetName(typeof(grr.Categories),1) // or just "Wildlife"