Sorry for getting this old thread up.
I would go the following way to localize enum, as it can display meaningful and localized values to user, not just description, through a dropdownlist text field in this example.
First, I create a simple method called OwToStringByCulture to get localized strings from a global resources file, in this example it is BiBongNet.resx in the App_GlobalResources folder. Inside this resource file, make sure you have all strings the same as the values of the enum (ReallyNice, SortOfNice, NotNice). In this method, I pass in the parameter: resourceClassName which is usually the name of the resource file.
Next, I create a static method to fill a dropdownlist with enum as its datasource, called OwFillDataWithEnum. This method can be used with any enum later.
Then in the page with a dropdownlist called DropDownList1, I set in the Page_Load the following just one simple line of code to fill the enum to the dropdownlist.
BiBongNet.OwFillDataWithEnum<HowNice>(DropDownList1, "BiBongNet");
That's it. I think with some simple methods like these, you can fill any list control with any enum, with not just as descriptive values but localized text to display. You can make all these methods as extension methods for better use.
Hope this help.
Share to get shared!
Here are the methods:
public class BiBongNet
{
enum HowNice
{
ReallyNice,
SortOfNice,
NotNice
}
/// <summary>
/// This method is for filling a listcontrol,
/// such as dropdownlist, listbox...
/// with an enum as the datasource.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ctrl"></param>
/// <param name="resourceClassName"></param>
public static void OwFillDataWithEnum<T>(ListControl ctrl, string resourceClassName)
{
var owType = typeof(T);
var values = Enum.GetValues(owType);
for (var i = 0; i < values.Length; i++)
{
//Localize this for displaying listcontrol's text field.
var text = OwToStringByCulture(resourceClassName, Enum.Parse(owType, values.GetValue(i).ToString()).ToString());
//This is for listcontrol's value field
var key = (Enum.Parse(owType, values.GetValue(i).ToString()));
//add values of enum to listcontrol.
ctrl.Items.Add(new ListItem(text, key.ToString()));
}
}
/// <summary>
/// Get localized strings.
/// </summary>
/// <param name="resourceClassName"></param>
/// <param name="resourceKey"></param>
/// <returns></returns>
public static string OwToStringByCulture(string resourceClassName, string resourceKey)
{
return (string)HttpContext.GetGlobalResourceObject(resourceClassName, resourceKey);
}
}