Jawad Khan

Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.
posts - 45, comments - 133, trackbacks - 155

My Links

News

Archives

Post Categories

Image Galleries

How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

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

Print | posted on Friday, June 24, 2005 12:05 PM | Filed Under [ ASP.NET ]

Feedback

Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

Many thanks - Spent ages trying to figure this out.
9/28/2005 2:22 AM | Hogan
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

please provide the vb syntax of ddColor.DataSource = Enum.GetNames(typeof(Color));
10/21/2005 12:37 PM | Morten
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

ddColor.DataSource = Enum.GetNames(GetType(Color))

Use the Code Conversion Utility posted in my Post here earlier for full conversion ..
10/22/2005 11:29 PM | JawadKhan
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

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.
11/9/2005 2:49 PM | bob
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

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.
11/14/2005 2:22 PM | StuartV
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

ps. And (in case it's not obvious), the hex numbers are values in an enum you have defined.
11/14/2005 2:23 PM | StuartV
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

The VB.NET syntax is actually slightly different:

ddColor.DataSource = [Enum].GetNames(GetType(Color))
4/3/2006 2:13 AM | Jack Stow
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

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 :-)
4/25/2006 8:57 AM | honzas
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

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
4/25/2006 8:59 AM | honzas
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....


C# - Enums
http://michaelsync.wordpress.com/2006/07/08/c-enums/
7/24/2006 4:17 AM | Mr-Google
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

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
9/1/2006 6:16 AM | Gilles
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

Thanks a lot!
5/14/2007 1:14 AM | Celine
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

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"))
3/26/2009 7:04 PM | Valamas
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

You can't modify while databinding
5/25/2009 8:08 PM | amr
Gravatar

# re: How to Bind Enum Types to the Dropdown or any other bindable Control in ASP.Net .....

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>
5/27/2009 2:25 PM | Jeremy

Post Comment

Title  
Name  
Email
Url
Comment   

Powered by: