You must have faced this type of scenario in your day to day life where you have to bind the enums into the DropDownList or ListBox control. A solution is given to this link
http://stackoverflow.com/questions/61953/how-do-you-bind-an-enum-to-a-dropdownlist-control-in-asp-net however it deosn't bind the dropDownList items value. It just binds the text and value of the item as the enum items name (at least thats what I got).
I have improved this code little further and below code brings the enum name as the item text and enum value as item value in the DropDownList.
Array itemValues = System.Enum.GetValues(typeof(NewsCategory));
Array itemNames = System.Enum.GetNames(typeof(NewsCategory));
for (int i = 0; i <= itemNames.Length - 1; i++)
{
var value = (int)(NewsCategory)Enum.Parse(typeof(NewsCategory), itemValues.GetValue(i).ToString(), true);
ListItem item = new ListItem(itemNames.GetValue(i).ToString(), value.ToString());
dropCategory.Items.Add(item);
}
In the above code, notice the value variable that I have retrieved from the enum by parsing it and the same is being set as the value for the ListItem.
If you have any better code than this, do respond to this thread with your code.
Hope this helps.
Thanks
Regards,
Sheo Narayan
http://www.dotnetfunda.com