Extending the DropDownList to Support Enums
page 2 of 6
by Steven Smith
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 32184/ 86

Binding a Standard DropDownList to an Enum

Before we delve into the derived DropDownList, consider the code that it is meant to replace.  Let's say that we have an enum that describes Status, and has possible values of Active, Inactive, Pending, and Declined.  We'll say these are for Proposal, so we'll call the enum ProposalStatus.

Listing 1: ProposalStatus enum

public enum ProposalStatus
  Active =1,
  Inactive = 2,
  Pending = 3,
  Declined = 4

Now if we want to render a DropDownList that uses this enum as its data source, we would use code like the following:

Listing 2: Binding Names to DropDownList

if (!Page.IsPostBack)
  ProposalStatusDropDownList.DataSource = 

This will render a DropDownList with display and value both as the name of the enum – the value is not stored in the DropDownList.  To get at the value, you would use code like this:

Listing 3: Getting a Strongly Typed Enum Value from the DropDownList

protected void Button1_Click(object sender, EventArgs e)
  ProposalStatus myStatus = (ProposalStatus)Enum.Parse(
    typeof(ProposalStatus), ProposalStatusDropDownList.SelectedValue);
  Label1.Text = myStatus.ToString();

Of course, if you actually want to store the enum’s numeric value as the value of the DropDownList, the most effective way to achieve that is to load up a SortedList<int,string> with the values and their corresponding names, and then DataBind to this collection.  This is easily done with a helper method like the one shown in Listing 4.  Note that since we cannot use where T:System.Enum the best we can do is constrain T to be a struct and then do a type check in code to verify it inherits from Enum.

Listing 4: Convert Enum to SortedList<string,int> Collection

public static SortedList<stringint> GetEnumDataSource<T>() where T:struct
    Type myEnumType = typeof(T);
    if (myEnumType.BaseType != typeof(Enum))
        throw new ArgumentException("Type T must inherit from System.Enum.");
    SortedList<stringint> returnCollection = new SortedList<stringint>();
    string[] enumNames = Enum.GetNames(myEnumType);
    for (int i = 0; i < enumNames.Length; i++)
            (int)Enum.Parse(myEnumType, enumNames[i]));
    return returnCollection;

Then to use this method with a DropDownList we would use code like what is shown in Listing 5.

Listing 5: Bind to SortedList from Enum

ProposalStatusDropDownList2.DataSource =
ProposalStatusDropDownList2.DataValueField = "Value";
ProposalStatusDropDownList2.DataTextField = "Key";

View Entire Article

User Comments

Title: Great   
Name: Suraj
Date: 2008-12-04 1:30:52 AM
great helped immensly
Title: Great   
Name: Willy AF
Date: 2008-02-02 10:01:36 PM
Great work, I will wait for the next trick, Very helpful,,,
Title: Greate tips   
Name: nguyen letan
Date: 2008-02-02 7:31:09 AM
Your article is very helpful for me. Thanks!
Title: Great Trick   
Name: s.k.shivaprasad
Date: 2008-01-31 4:47:50 AM
Great work. I spent a lot of time trying to figure this out myself. It is kind of annoying that ASP.NET doesn't support declared generic controls. That would be a great improvement.good...
Title: Great Article   
Name: Joydip
Date: 2007-12-10 12:39:26 PM
Hi Steve, this is an excellent article. This will help the readers a lot. Awesome!



Author, Data Presentation Controls Essentials (PacktPub)
Title: Really useful   
Name: Uday D
Date: 2007-12-05 3:42:07 AM
Very helpful link. Thanks.
Title: Great Trick   
Name: Brendan
Date: 2007-12-04 9:53:13 AM
Great work. I spent a lot of time trying to figure this out myself. It is kind of annoying that ASP.NET doesn't support declared generic controls. That would be a great improvement.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023 ASPAlliance.com  |  Page Processed at 2023-09-28 4:30:57 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search