开发者

set enum from dropdownlist

开发者 https://www.devze.com 2023-01-26 02:52 出处:网络
I have a property on my customer object, say gender, based on an enum. How do I set the enum property on the object based on a dropdownlist.selec开发者_StackOverflowtedValue?

I have a property on my customer object, say gender, based on an enum.

How do I set the enum property on the object based on a dropdownlist.selec开发者_StackOverflowtedValue?

Thanks, rod.


You can get the list of Enum names using something like this:

string[] genderList = Enum.GetNames(typeof(gender));

from there, just populate the drop down list

to parse it back to the enum type...

Gender gender = (Gender)Enum.Parse(typeof(Gender), ddl.SelectedValue);


public enum Gender
{
    Male = 0,
    Female
}

Gender gender = (Gender)dropdownlist.selectedValue

The values of dropdown list can be 0,1 or Male,Female.


You can check Enum.Parse It takes the type and the string, or you can make your own converter

0

精彩评论

暂无评论...
验证码 换一张
取 消