Dropdown databind to public enum

W

wapsiii

I'm trying to do this
http://geekswithblogs.net/jawad/archive/2005/06/24/EnumDropDown.aspx

public enum Color
{
RED,
GREEN,
BLUE
}

protected System.Web.UI.WebControls.DropDownList ddColor;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
ddColor.DataSource = Enum.GetNames(typeof(Color));
ddColor.DataBind();
}
}

private void ddColor_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Color selectedColor = (Color)Enum.Parse(ddColor.SelectedValue);
}

in vb, but I can't get the syntax of "ddColor.DataSource =
Enum.GetNames(typeof(Color));" correct!

Please help
 
G

Guest

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not MyBase.IsPostBack Then
Me.ddColor.DataSource = Enum.GetNames(GetType(Color))
Me.ddColor.DataBind
End If
End Sub

Your event handler is wrong, however (missing one arg on the enum.parse):

Private Sub ddColor_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As
EventArgs)
Dim color1 As Color = CType(Enum.Parse(GetType(Color), _

ddColor.SelectedValue), Color)
End Sub





--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 

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

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top