Retrieving selected enum from dropdown list

I am binding an enumeration to a dropdown list in asp.net 4.0 C#

Enumernation is:

public enum Frequency
{
    [Description("Select a frequency")]
    None,

    [Description("Every Hour/Mintues")]
    EveryHourOrMintues, 

    [Description("Previous Day Data")]
    PreviousDayData,

    [Description("Once a week")]
    OnceaWeek
}

On the selection of a value from the dropdown I want to get the enum value in return: I am doing it like:

Frequency selectedFrequency;
foreach (Frequency f in Enum.GetValues(typeof(Frequency)))
{
    if (f.ToString().Equals(this.dropDownListFrequency.SelectedValue))
    {
        selectedFrequency = f;
        break;
    }
}

It is working but definitely a poor way I guess, by looping through each of the items in enum (even though enum is very small)

How can I retrieve selected enum like:

Frequency selectedValue = Enum.GetValues(typeof(Frequency)).Cast<Frequency>().Select(f => f.ToString().Equals(this.dropDownListFrequency.SelectedValue));

I understand that above given code has casting issue.

Edit For more information, here is how I am binding enum to dropdown list

var frequencies =  Enum.GetValues(typeof(Frequency)).Cast<Frequency>().Select(f => new
            {
                Text = f.ToDescriptiveTextUsingAttributes(),
                Value = f.ToString()
            });
this.dropDownListFrequency.DataSource=frequencies ;
this.dropDownListFrequency.DataTextField = "Text";
this.dropDownListFrequency.DataValueField = "Value";

ToDescriptiveTextUsingAttributes() is an extension method that returns value of Description attribute of enum


If the value of the dropdown list is the enum's integer representation (eg 0,1,2...), then you can simply cast it back to the enum:

Frequency f = (Frequency)int.Parse(dropDownListFrequency.SelectedValue);

If the value of the dropdown list is the enum's string representation (eg "None", "EveryHourOrMintues"...), then you can use Enum.Parse() :

Frequency f = (Frequency)Enum.Parse(
    typeof(Frequency), dropDownListFrequency.SelectedValue);

You can extend the ListItem class to store the actual Frequency enum object and add those specialized ListItem objects to dropDownListFrequency.Items . When you retrieve dropdownListFrequency.SelectedItem you can get the actual value selected.

class FrequencyListItem : System.Web.UI.WebControls.ListItem.ListItem {
    private Frequency _Frequency;

    public Frequency Frequency {
        get { return _Frequency }
    }

    public FrequencyListItem (Frequency f) {
        this._Frequency = f;
        this.Text = f.ToDescriptiveTextUsingAttributes();
        this.Value = f.ToString();
    }
}

Since all your elements in the Items property are your specialized class, you can easily retrieve your enum value as follows:

Frequency selectedFrequency = ((FrequencyListItem )this.dropDownListFrequency.SelectedItem).Frequency;

Edit: You can still even use the same binding model you have with some minor changes:

var frequencies =  Enum.GetValues(typeof(Frequency)).Cast<Frequency>().Select(f => new FrequencyListItem()
            {
                //FrequencyListItem will auto set this in the constructor for you!
                //Text = f.ToDescriptiveTextUsingAttributes(),
                //Value = f.ToString()
            });
this.dropDownListFrequency.DataSource=frequencies;
this.dropDownListFrequency.DataTextField = "Text";
this.dropDownListFrequency.DataValueField = "Value";
链接地址: http://www.djcxy.com/p/48128.html

上一篇: 如何从ASP.NET MVC中的枚举创建下拉列表?

下一篇: 从下拉列表中检索选定的枚举