Enum - ArrayList to DropDownList

G

Greg

I have the following Enum I would like to display in an <asp:DropDownList>
control. I have no problem getting the names to appear using the .GetNames
method. Thus, None, Male and Female appear in the drop down list as desired.

Pulbick Enum Gender
{
None = 0,
Male = 1,
Female = 2
}

What I want to achieve though is retrieve the assiciated number/id related
to each option so that I can save it to my database. How can I get the value
1, when Male is selected in the DropDownList control?

Thanks
 
J

Jesse Houwing

Hello greg,
I have the following Enum I would like to display in an
<asp:DropDownList> control. I have no problem getting the names to
appear using the .GetNames method. Thus, None, Male and Female appear
in the drop down list as desired.

Pulbick Enum Gender
{
None = 0,
Male = 1,
Female = 2
}
What I want to achieve though is retrieve the assiciated number/id
related to each option so that I can save it to my database. How can I
get the value 1, when Male is selected in the DropDownList control?

Thanks


You can use

(int)Enum.Parse(string, typof(Gender)))

To get the associated enum value.

Even better would be to bind not just the names to the dropdown, but the
name/valeu pairs of course, that would forego the whole parsing of strings
issue...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top