Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 30,052 times

Related Categories

Binding a Control to an Enum

Imagine you have an enum defined in your code like so:

enum ContractType
{
    Permanent = 1,
    Contract = 2,
    Internship = 99
}

Suppose we have a DropDownList on an ASP.NET page from which we'd like the user to select the appropriate ContractType.

<asp:DropDownList runat="server" DataTextField="Key" DataValueField="Value" id="MyDropDownList">

Obviously, we can't do something as simple as

MyDropDownList.DataSource = ContractType;

as ContractType is a type, not an object. Instead, we can create a simple helper function that uses the Enum.GetValues and Enum.GetNames methods in order to create a Hashtable object (consisting of keys - Permanent, Contract, etc, and values - 1, 2 etc).

public static Hashtable BindToEnum(Type enumType)
{
    // get the names from the enumeration
    string[] names = Enum.GetNames(enumType);
    // get the values from the enumeration
    Array values = Enum.GetValues(enumType);
    // turn it into a hash table
    Hashtable ht = new Hashtable();
    for (int i = 0; i < names.Length; i++)
        // note the cast to integer here is important
        // otherwise we'll just get the enum string back again
        ht.Add(names[i], (int)values.GetValue(i));
    // return the dictionary to be bound to
    return ht;
}

You can then use this as follows:

MyDropDownList.DataSource = BindToEnum(typeof(ContractType));

and we get a drop down list bound to the appropriate names/values as defined by the enum (note that we specified DataTextField and DataValueField in the earlier DropDownList definition).

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [4713] Binding a Control to an Enum

    Posted by Vphillips on 07 Aug 2007

    Why not just use:


    MyDropDownList.DataSource = Enum.GetNames(typeof

  • Re: [4713] Binding a Control to an Enum

    Posted by alok_bunty2002 on 22 Feb 2007

    I am binding an ASP.net control using the above methods. For some reason each of the item in dropdownlist is showing System.Collections.DictionaryEntry as text and value, although I checked the key va...

  • Re: How do u set the order in dropdownList

    Posted by ezztek on 07 Dec 2006

    In order to be able to sort you will have to implement an instance of the icomparer. 


    The first change is to the "BindToEnum" function:<...

  • Re:

    Posted by ezztek on 06 Dec 2006

    Good Post!!


    Just wanted to make an additional comment.


    By changing the following lines:


    Dim names As String() = ContractType.GetNames(enumType) 
    Dim val...

  • Instead use an arraylist

    Posted by rajeev2005 on 13 Jan 2006

    Using Hashtable it is not possible.

    Instead of HashTable use an ArrayList

    ht=new ArrayList();
    And change the code like this.

    ht.Insert((int)values.GetValue(i),names[i]);

    No need to speci...