Convert DropDownList values to enum values

A

Andy B.

I have the following dropdownlist and enum. I want the listItem values for
the dropdownlist to be the enums values. How do you do this?

-- dropdownlist
<asp:DropDownList ID="HeadlinesTypeDropDown" runat="server">

<asp:ListItem Selected="True" Value="1">All</asp:ListItem>

<asp:ListItem Value="2">Current</asp:ListItem>

<asp:ListItem Value="3">Expired</asp:ListItem>

</asp:DropDownList>



--enum

public enum HeadlineType

All=1

Current=2

Expired=3

end enum
 
J

Jesse Houwing

Hello Andy B.,
I have the following dropdownlist and enum. I want the listItem values
for the dropdownlist to be the enums values. How do you do this?

<asp:ListItem Selected="True" Value="1">All</asp:ListItem>

<asp:ListItem Value="2">Current</asp:ListItem>

<asp:ListItem Value="3">Expired</asp:ListItem>

</asp:DropDownList>

--enum

public enum HeadlineType

All=1

Current=2

Expired=3

end enum


You can use

HeadlineType value = (HeadlineType)Enum.Parse(string, typeof(HeadlineType))

to do this.

I'm unfamiliar with the exact VB.NET syntax though...

I guess it would be somthing like:

Dim value as HeadlineType = CType(Enum.Parse(string, typename(HeadlineType)),
HeadlineType)
 
A

Alexey Smirnov

I have the following dropdownlist and enum. I want the listItem values for
the dropdownlist to be the enums values. How do you do this?

-- dropdownlist
<asp:DropDownList ID="HeadlinesTypeDropDown" runat="server">

<asp:ListItem Selected="True" Value="1">All</asp:ListItem>

<asp:ListItem Value="2">Current</asp:ListItem>

<asp:ListItem Value="3">Expired</asp:ListItem>

</asp:DropDownList>

--enum

public enum HeadlineType

All=1

Current=2

Expired=3

end enum

Select Case HeadlinesTypeDropDown.Selected.Value
Case HeadlineType. All
Return "great"
Case HeadlineType.Current
Return "good"
....
 

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