INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Populate ASP Dropdown list with enum Description and Values

Populate ASP Dropdown list with enum Description and Values

Populate ASP Dropdown list with enum Description and Values

(OP)
Hi Guys,

I am trying to create a dropdown list in asp.net using an enum as the data for the list. what I am wanting is to have the description set as the dropdownlist text and the value of the enum set as the value for the dropdown list.

So far I have found two snippets of code the first gets the enum names and values

CODE --> C#

Array itemValues = System.Enum.GetValues(typeof(CountryList));
Array itemNames = System.Enum.GetNames(typeof(CountryList));

for (int i = 0; i <= itemNames.Length - 1; i++)
{

    var value = (int)(CountryList)Enum.Parse(typeof(CountryList), itemValues.GetValue(i).ToString(), true);

    ListItem item = new ListItem(itemNames.GetValue(i).ToString(), value.ToString());

    ddlCultureList.Items.Add(item);

} 

and I can get the Enum description using the following

CODE --> C#

protected void Page_Load(object sender, EventArgs e)
        {
            //Get attributes from the enum
            var items =
               typeof(CountryList).GetEnumNames()
                .Select(x => typeof(CountryList).GetMember(x)[0].GetCustomAttributes(
                   typeof(System.ComponentModel.DescriptionAttribute), false))
                .SelectMany(x =>
                   x.Select(y => new ListItem(((System.ComponentModel.DescriptionAttribute)y).Description)));

            //Add items to ddl
            foreach (var item in items)
                ddlCultureList.Items.Add(item);
        } 

I just can't figure out how to get both together so the enum description (United Kingdom) populates in the drop down list text and GB is set as the value as I want to use the value for another part of my application.

An example of the Enum I am using is

CODE --> C#

public enum CountryList
    {
        [System.ComponentModel.Description("United States")]
        US,
        [System.ComponentModel.Description("United Kingdom")]
        GB
    } 

any help and tips would be appreciated. I was originally going to use CultureInfo and RegionInfo to get the list of Countries, however it brings back a load of countries that my company does not do business with so the enum was a better option. Plus I feel I could use this for future projects.

Many Thanks

J.

Regards

J.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close