Convert DropDownList values to enum values

  • Thread starter Thread starter Andy B.
  • Start date Start date
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
 
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)
 
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"
....
 
Back
Top