Bind Enum Data Type into DropdownList control in Asp.Net C# VB.Net

In some cases we need to bind Enum data into a DropdownList. So in this article or tutorial I will explain “How one can bind Enum into a DropdownList in Asp.net“. After searching a lot of time, I found all codes are inline but I want to control enum from a single class for better manageability & re usability. As a developer we need Enum in most cases like Status,Type etc. So that I want to encapsulate all my necessary Enums of my project into a single class. Here I will show you how you can easily manage Enums Data Type in your project and use multiple times in your pages whenever required.

We can declare Enum in two ways. One is we can define value when we create an Enum on other hand we do not need to declare values. Please check the both types of different declaration from given below:

Enum Data Type Declaration Syntax in C#:

    public enum TaskType
    {
        Development,
        Operation,
        Reporting,
        Monitoring
    }

    public enum Status
    {
        New = 100,
        Started = 200,
        PartiallyCompleted = 35,
        Completed = 400,
        Cancelled = 500
    }

Enum Data Type Declaration Syntax in VB.Net:

    Public Enum TaskType
        Development
        Operation
        Reporting
        Monitoring
    End Enum

    Public Enum Status
        [New] = 100
        Started = 200
        PartiallyCompleted = 35
        Completed = 400
        Cancelled = 500
    End Enum

Note: In the first type of declaration the value starts from 0 implicitly. But in the second type of declaration of Enum we have declared the values instead of implicit declaration which is termed as explicit declaration.

To Test the Example Find the Below HTML Markup:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>
    <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br />
    <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList><br />
    <asp:Button ID="Button1" runat="server" text="Get Selected Enum Value" 
        onclick="Button1_Click" /><br />
    <hr />
    <asp:Label ID="Label1" runat="server" Font-Bold="true"></asp:Label><br />
    <asp:Label ID="Label2" runat="server" Font-Bold="true"></asp:Label>
</div>
</asp:Content>

How to Bind those types of Enum into our DropDownList Control:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = Enum.GetNames(typeof(EnumClass.TaskType));
            DropDownList1.DataBind();

            DropDownList2.DataSource = Enum.GetNames(typeof(EnumClass.Status));
            DropDownList2.DataBind();
        }
    }

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            DropDownList1.DataSource = [Enum].GetNames(GetType(EnumClass.TaskType))
            DropDownList1.DataBind()

            DropDownList2.DataSource = [Enum].GetNames(GetType(EnumClass.Status))
            DropDownList2.DataBind()
        End If
    End Sub

Get corresponding Enum value from DropDownList Selected Item & Selected Index:
C# Code:

    protected void Button1_Click(object sender, EventArgs e)
    {
        EnumClass.TaskType SelectedType = (EnumClass.TaskType)Enum.Parse(typeof(EnumClass.TaskType), DropDownList1.SelectedValue);
        Label1.Text = "Task Type:  SelectedText: " + SelectedType.ToString() + ">>  SelectedValue: " + SelectedType.GetHashCode();

        EnumClass.Status SelectedStatus = (EnumClass.Status)Enum.Parse(typeof(EnumClass.Status), DropDownList2.SelectedValue);
        Label2.Text = "Task Status:  SelectedText: " + SelectedStatus.ToString() + ">>  SelectedValue: " + SelectedStatus.GetHashCode();
    }

VB.Net Code:

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim SelectedType As EnumClass.TaskType = DirectCast([Enum].Parse(GetType(EnumClass.TaskType), DropDownList1.SelectedValue), EnumClass.TaskType)
        Label1.Text = "Task Type:  SelectedText: " & SelectedType.ToString() & ">>  SelectedValue: " & SelectedType.GetHashCode()

        Dim SelectedStatus As EnumClass.Status = DirectCast([Enum].Parse(GetType(EnumClass.Status), DropDownList2.SelectedValue), EnumClass.Status)
        Label2.Text = "Task Status:  SelectedText: " & SelectedStatus.ToString() & ">>  SelectedValue: " & SelectedStatus.GetHashCode()
    End Sub

Output:
Bind Enum Types to a DropdownList control in asp.net Csharp VB.Net

If you can not understand what I am telling then follow the below complete example:
Create a new project. Add a new class & give the classname as “EnumClass”. Now paste the below codes into the class:
C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for EnumClass
/// </summary>
public class EnumClass
{
	public EnumClass()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public enum TaskType
    {
        Development,
        Operation,
        Reporting,
        Monitoring
    }

    public enum Status
    {
        New = 100,
        Started = 200,
        PartiallyCompleted = 35,
        Completed = 400,
        Cancelled = 500
    }
}

VB.Net Code:

Imports Microsoft.VisualBasic

Public Class EnumClass
    Public Enum TaskType
        Development
        Operation
        Reporting
        Monitoring
    End Enum

    Public Enum Status
        [New] = 100
        Started = 200
        PartiallyCompleted = 35
        Completed = 400
        Cancelled = 500
    End Enum
End Class

Now copy & paste the below HTML markup code:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>
    <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br />
    <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList><br />
    <asp:Button ID="Button1" runat="server" text="Get Selected Enum Value" 
        onclick="Button1_Click" /><br />
    <hr />
    <asp:Label ID="Label1" runat="server" Font-Bold="true"></asp:Label><br />
    <asp:Label ID="Label2" runat="server" Font-Bold="true"></asp:Label>
</div>
</asp:Content>

Now in code behind write the below Code:
C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = Enum.GetNames(typeof(EnumClass.TaskType));
            DropDownList1.DataBind();

            DropDownList2.DataSource = Enum.GetNames(typeof(EnumClass.Status));
            DropDownList2.DataBind();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        EnumClass.TaskType SelectedType = (EnumClass.TaskType)Enum.Parse(typeof(EnumClass.TaskType), DropDownList1.SelectedValue);
        Label1.Text = "Task Type:  SelectedText: " + SelectedType.ToString() + ">>  SelectedValue: " + SelectedType.GetHashCode();

        EnumClass.Status SelectedStatus = (EnumClass.Status)Enum.Parse(typeof(EnumClass.Status), DropDownList2.SelectedValue);
        Label2.Text = "Task Status:  SelectedText: " + SelectedStatus.ToString() + ">>  SelectedValue: " + SelectedStatus.GetHashCode();
    }
}

VB.Net Code:

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim SelectedType As EnumClass.TaskType = DirectCast([Enum].Parse(GetType(EnumClass.TaskType), DropDownList1.SelectedValue), EnumClass.TaskType)
        Label1.Text = "Task Type:  SelectedText: " & SelectedType.ToString() & ">>  SelectedValue: " & SelectedType.GetHashCode()

        Dim SelectedStatus As EnumClass.Status = DirectCast([Enum].Parse(GetType(EnumClass.Status), DropDownList2.SelectedValue), EnumClass.Status)
        Label2.Text = "Task Status:  SelectedText: " & SelectedStatus.ToString() & ">>  SelectedValue: " & SelectedStatus.GetHashCode()
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            DropDownList1.DataSource = [Enum].GetNames(GetType(EnumClass.TaskType))
            DropDownList1.DataBind()

            DropDownList2.DataSource = [Enum].GetNames(GetType(EnumClass.Status))
            DropDownList2.DataBind()
        End If
    End Sub
End Class

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*