In this article I will explain with an example, how to populate (bind) DropDownList from Enum in ASP.Net using C# and VB.Net.
The Enum values will be extracted into an Array and then each Array item will be added to the ASP.Net DropDownList using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net DropDownList and a Button control.
<asp:DropDownList ID="ddlColors" runat="server">
</asp:DropDownList>
<asp:Button runat="server" Text="Submit" OnClick="OnSubmit" />
 
 
Enum
Following Enum consists of 3 Colors i.e. Red, Green and Blue to which the values are set to 1, 2 and 3 respectively.
C#
public enum Colors
{
    Red = 1,
    Blue = 2,
    Green = 3
}
 
VB.Net
Public Enum Colors
    Red = 1
    Blue = 2
    Green = 3
End Enum
 
 
Populating (Binding) DropDownList from Enum in ASP.Net
Inside the Page Load event handler, the values of the Enum are fetched into an Array and then a loop is executed over the Array items and one by one each item is added to the DropDownList.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        Array colors = Enum.GetValues(typeof(Colors));
        foreach (Colors color in colors)
        {
            ddlColors.Items.Add(new ListItem(color.ToString(), ((int)color).ToString()));
        }
       ddlColors.Items.Insert(0, newListItem("--Select Color--", ""));
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgsHandles Me.Load
    If Not Me.IsPostBack Then
        Dim colors As Array = [Enum].GetValues(GetType(Colors))
        For Each color As Colors In colors
            ddlColors.Items.Add(New ListItem(color.ToString(), CInt(color).ToString()))
        Next
       ddlColors.Items.Insert(0, New ListItem("--Select Color--", ""))
    End If
End Sub
 
 
Displaying selected Text and Value
When the Submit Button is clicked, following event handler is executed.
Inside this event handler, the selected text and value of DropDownList are fetched.
Finally, the fetched text and value are displayed using JavaScript Alert Message Box using the RegisterClientScriptBlock function.
C#
protected void OnSubmit(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(ddlColors.SelectedItem.Value))
    {
        string color = ddlColors.SelectedItem.Text;
        string id = ddlColors.SelectedItem.Value;
        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + "Color: " + color + "\\nID: " + id + "')", true);
    }
}
 
VB.Net
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
    If Not String.IsNullOrEmpty(ddlColors.SelectedItem.Value) Then
        Dim color As String = ddlColors.SelectedItem.Text
        Dim id As String = ddlColors.SelectedItem.Value
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & "Color: " & color & "\nID: " & id & "')", True)
    End If
End Sub
 
 
Screenshot
Populate (Bind) DropDownList from Enum in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads