Loading DropDownList from Enumeration in ASP.NET

Introduction:

This article shows how to load DropDownList from Enumeration using extension methods. Example in this demo loads DropDownlists from DayOfWeek and SpecialFolders Enumerations respectively.

Background:

Extension Methods often helps extend base functionality of an object by refactoring code. Unfortunately, Classes such as System.Enum, System.Object cannot be extended like a string or integer type. There are different ways to convert Enumeration to a List but I prefer using Enum GetNames and GetValues methods to make a list.

Markup:

html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <table style="font-familyTahomafont-sizesmall;">
        <tr>
            <td>
                Day:
            </td>
            <td>
                <asp:DropDownList ID="ddlWeekOfDay" Width="200px" runat="server" DataTextField="Text"
                    DataValueField="Value" 
                    onchange="weekOfDay.innerHTML='Selected Item: '+ 
                              this.options[this.selectedIndex].text+
                              '('+this.options[this.selectedIndex].value+')';">
                </asp:DropDownList>
            </td>
            <td>
                <span id="weekOfDay"></span>
            </td>
        </tr>
        <tr>
            <td>
                Special Folder:
            </td>
            <td>
                <asp:DropDownList ID="ddlSpecialFolder" runat="server" DataTextField="Text" DataValueField="Value"
                    Width="200px" 
                    onchange="specialFolder.innerHTML='Selected Item: '+ 
                              this.options[this.selectedIndex].text+
                              '('+this.options[this.selectedIndex].value+')';">
                </asp:DropDownList>
            </td>
            <td>
                <span id="specialFolder"></span>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
CodeBehind:


using System;
using System.Linq;
//Extension methods 
public static class EnumHelper 
{ 
    public static object ToList(this Enum T) 
    {
        Array values = Enum.GetValues(T.GetType()); 
        return Enum.GetNames(T.GetType()).ToList().Select((a, index) => new {
            Text = a, Value = (Int32)values.GetValue(index) }) .OrderBy(a=>a.Text); 
    } 
} 
protected void Page_Load(object sender, EventArgs e) 
{
    if (!IsPostBack) 
    { 
        ddlWeekOfDay.DataSource = DayOfWeek.Friday.ToList(); 
        ddlWeekOfDay.DataBind(); 
        ddlSpecialFolder.DataSource = Environment.SpecialFolder.AdminTools.ToList(); 
        ddlSpecialFolder.DataBind(); 
    } 
}
Conclusion:
Another option is adding ListItems to DropDownList by looping Enumeration Values. Type can also be extended instead of Enum.

Comments

Popular posts from this blog

Delete Confirmation for Delete ButtonField, TemplateField and CommandField in a GridView in ASP.NET

Selecting/Deselecting all CheckBoxes inside a ListView In ASP.NET

How to Create 3 Tier Vb.Net Desktop Application