How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net …..
While working on a Data Form in an ASP.Net application you might want to get a value from the user that corresponds to the Enum you created in your Business Layer. Since Enum types are not strings or .ToString() function doesn’t work directly with Enums you need to do it in slightly different way …
Lets take an example …
public enum Color
Every Enum type derives from System.Enum. There are two static methods that help bind data to a drop-down list control (and retrieve the value). These are Enum.GetNames and Enum.Parse. Using GetNames, you are able to bind to your drop-down list control as follows:
protected System.Web.UI.WebControls.DropDownList ddColor;
private void Page_Load(object sender, System.EventArgs e)
ddColor.DataSource = Enum.GetNames(typeof(Color));
Now if you want the Enum value Back on Selection ….
private void ddColor_SelectedIndexChanged(object sender, System.EventArgs e)
Color selectedColor = (Color)Enum.Parse(ddColor.SelectedValue);