Return to Snippet

Revision: 24607
at March 5, 2010 01:41 by emregulcan


Initial Code
enum Speed { 
        Low = 1, 
        Medium = 2, 
        High = 3 
    }
    
    protected void Page_Load(object sender, EventArgs e) { 
        Hashtable htSpeed = BindEnum(typeof(Speed)); 
        DropDownList1.DataSource = htSpeed; 
        DropDownList1.DataValueField = "key"; 
        DropDownList1.DataTextField = "value"; 
        DropDownList1.DataBind();
    }
    
    protected Hashtable BindEnum(Type speedEnum) { 
        string[] enumNm = Enum.GetNames(speedEnum); 
        int[] enumVal = (int[])Enum.GetValues(speedEnum); 
        Hashtable htSpeed = new Hashtable(); 
        for (int cnt = 0; cnt < enumVal.Length; cnt++) 
        { 
            htSpeed.Add(enumVal.GetValue(cnt), enumNm[cnt]); 
        }
        return htSpeed; 
    }

Initial URL


Initial Description


Initial Title
How to Bind An Enum Name and Value to an ASP.NET DropDownList

Initial Tags


Initial Language
C#