In the previous post where I showed how to bing a dropdown to a Enumeration. Here, I will show how to get the enum value of a string and make it the selectedindex of a dropdown list.

Suppose I have a enumeration like the one showed below:

public static class BusinessLogic
{
 public enum SetEnvironment
 {
    Development = 1,
    Testing = 2,
    Staging = 3,
    Production = 4
 };
}

If I have a dropdown DropDownList1 whose selecteditem needs to be “staging” at run time, I can do the below procedure to convert a string to a enum value:

string strSelect = “Staging”;
/*ClassName.EnumName EnumInstance = (ClassName.EnumNme)Enum.Parse(typeof(ClassName.EnumName), selectedtext);*/
BusinessLogic.SetEnvironment EnumInstance = (BusinessLogic.SetEnvironment)Enum.Parse(typeof(BusinessLogic.SetEnvironment), strSelect);
int intEnumVal = (int)EnumInstance;
DropDownList1.SelectedIndex = intEnumVal – 1;

Advertisements