Geeks With Blogs
Jawad Khan Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.

While working on a Data Form in an ASP.Net application you might want to get a value from the user that corresponds to the Enum you created in your Business Layer. Since Enum types are not strings or .ToString() function doesn't work directly with Enums you need to do it in slightly different way ...

Solution:

Lets take an example ...

public enum Color
{
    RED,
    GREEN,
    BLUE
}

  Every Enum type derives from System.Enum. There are two static methods that help bind data to a drop-down list control (and retrieve the value). These are Enum.GetNames and Enum.Parse. Using GetNames, you are able to bind to your drop-down list control as follows:

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();
     }
}

Now if you want the Enum value Back on Selection ....

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

Posted on Friday, June 24, 2005 12:05 PM ASP.NET | Back to top


Comments on this post: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Many thanks - Spent ages trying to figure this out.
Left by Hogan on Sep 28, 2005 2:22 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
please provide the vb syntax of ddColor.DataSource = Enum.GetNames(typeof(Color));
Left by Morten on Oct 21, 2005 12:37 PM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
ddColor.DataSource = Enum.GetNames(GetType(Color))

Use the Code Conversion Utility posted in my Post here earlier for full conversion ..
Left by JawadKhan on Oct 22, 2005 11:29 PM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
As a note, apparently not all ListControls are created equal. This works great for the drop down list, but does not work with RadioButtonLists, if that is your preference. They tend to behave oddly when binding this way (the SelectedIndex is not set when the radio button is selected). I was at least able to see that my approach was correct by seeing this example - it just was not working for the control I was trying to use.
Left by bob on Nov 09, 2005 2:49 PM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
How can you do this where the enum provides the values, but you provide separate display text? E.g. the list displays: "All", "Red Only", "Green Only", "Blue Only". But the values you get back are: 0x00ffffff, 0x00ff0000, 0x0000ff00, 0x000000ff.
Left by StuartV on Nov 14, 2005 2:22 PM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
ps. And (in case it's not obvious), the hex numbers are values in an enum you have defined.
Left by StuartV on Nov 14, 2005 2:23 PM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
The VB.NET syntax is actually slightly different:

ddColor.DataSource = [Enum].GetNames(GetType(Color))
Left by Jack Stow on Apr 03, 2006 2:13 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Try to use SortedList (or HashTable) as data source - you should fill it with values and their codes.

There is an article at http://weblog.rebex.cz/admin/blogs/posteditor.aspx?App=honzas&PostID=1289
(unfortunately not written in English, but the code in c# should be understandable :-)
Left by honzas on Apr 25, 2006 8:57 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
ps: Oh, sorry, I put an internal hyperlink above, the right is this one: http://weblog.rebex.cz/blogs/honzas/archive/2006/04/25/1289.aspx
Left by honzas on Apr 25, 2006 8:59 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Left by Mr-Google on Jul 24, 2006 4:17 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Another way to do it without databinding which would allow to add empty values

public static void PopulateDropdownlistFromEnum(DropDownList pDDL, System.Type enumType, string pColValue, bool addEmptyValue)
{
pDDL.Items.Clear();

Array values = Enum.GetValues(enumType);

if (addEmptyValue)
pDDL.Items.Add(new ListItem("", "-1")) ;

for(int i=0;i < values.Length;i++)
{
int key = Convert.ToInt32(Enum.Parse(enumType, values.GetValue(i).ToString())) ;
string text = Convert.ToString(Enum.Parse(enumType, values.GetValue(i).ToString()));
pDDL.Items.Add(new ListItem(text, key.ToString()));
}

if (pColValue != "")
pDDL.SelectedValue = Enum.Parse(enumType, pColValue).ToString();
}

Gilles
Left by Gilles on Sep 01, 2006 6:16 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Thanks a lot!
Left by Celine on May 14, 2007 1:14 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
To add an empty value item at the top or bottom or where ever, use INSERT.

Simply
ddColor.DataSource = Enum.GetNames(typeof(Color));
ddColor.DataBind();

then something like
ddColor.Items.Insert(0, new ListItem("NEW TOP ITEM"))
Left by Valamas on Mar 26, 2009 7:04 PM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
You can't modify while databinding
Left by amr on May 25, 2009 8:08 PM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
You can have a list item in the ddl already and then append the databound items if you want an empty item.

<asp:ListBox runat="server" ID="myDDL" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="" />
</asp:ListBox>
Left by Jeremy on May 27, 2009 2:25 PM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
I receive an error on:

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

Had to change it to:
[code]

Color selectedColor = (Color)Enum.Parse(typeof(Color),ddColor.SelectedValue);

[/code]
Left by Fase on Jul 06, 2009 7:58 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Great information...
Thanks
Left by Mahmoud Hosney on Aug 24, 2009 12:50 PM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Very good solution...
I was searching a simple solution for this problem...
For the moment it's the better.

Muchas gracias ;)
Left by Cristian Market on Sep 24, 2009 10:56 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Cool, just the job, thanks
Mickey
Left by Mickey on Oct 07, 2009 5:06 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Im starting to figure out that enums are the worst part of .net. I think im going to move everything over to a dictionary or something. fun.
Left by imax theater locations on Aug 14, 2010 10:37 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Complete explanation in both Asp.Net and winforms tutorial:

http://www.docstorus.com/viewer.aspx?code=833d27e2-5e3b-4ec0-b950-02c8fc2ec572&title=How%20to%20use%20Enum%20with%20Combobox%20in%20C#%20WinForms%20and%20Asp.Net
Left by justin on Apr 09, 2011 5:06 PM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
very gud sample..
thanks a lot....
Left by san on Jun 07, 2011 1:19 AM

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....
Requesting Gravatar...
Then, how do you bind the Enum to an ASP.NET Repeater control? How you Eval values of the bound list?
Left by Saeed Neamati on Dec 19, 2011 3:37 AM

Your comment:
 (will show your gravatar)


Copyright © Jawad Khan | Powered by: GeeksWithBlogs.net | Join free