Thursday, March 25, 2010

Bind Enum Types to a DropdownList control in asp.net C#




Please visit my new Web Site WWW.Codedisplay.com



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. So that i can manage enums from a single file. 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 in your project and use multiple times in your pages where 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 decalere value. Please check the two types of different declaration from given below:



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

    public enum Status
    {
        New=100,
        Started=200,
        PartiallyCompleted=35,
        Completed=400,
        Cancelled=500
    }
In the first type of declaration the value starts from 0 implicitly. But in the second type of decalaration of Enum we have declared the values instead of implicit declaration which is termed as explicit declatration. Now our concern is how we can bind those types of enum into our dropdownlist. To do this follow the below code sample:
DropDownList1.DataSource = Enum.GetNames(typeof(EnumClass.TaskType));
            DropDownList1.DataBind();

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

Now enum type data bindinng is completed. Now if we want to read the corresponding enum value based on dropdownlist selection then we can follow the code sample:
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();

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:
using System;

public class EnumClass
{

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

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

    public EnumClass()
 {
 }
}

Now add a webpage into your project and paste the below HTML markup code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Bind_Enum.aspx.cs" Inherits="Bind_Enum" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>How to bind enum into dropdownlist</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
    <br /><br />
    <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
    <br /><br />
    <asp:Button ID="Button1" runat="server" text="Get Selected Enum Value" OnClick="Button1_Click" />
    <br /><br />
    <hr />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>
Now under page_load event write the below 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();
        }

    }
Since when you run the page and click on the button control i want to read the both selected text and selected value so that under the button click event paste the below 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();
    }
Now run the project and you will get the below output:

Bind Enum Value

Hope now you can easily manage all enum types from a single class file and use in your pages when required.
Happy coding !!

3 comments:

Anonymous said...

well , very god

Anonymous said...

very very gooooooooooooooood.............

Anonymous said...

Good Coding.......

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash