Reputation: 2328
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
Upvotes: 2
Views: 3267
Reputation: 1206
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";
Upvotes: 0
Reputation: 96561
If the value of the dropdown list is the enum's integer representation (e.g. 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 (e.g. "None", "EveryHourOrMintues"...), then you can use Enum.Parse()
:
Frequency f = (Frequency)Enum.Parse(
typeof(Frequency), dropDownListFrequency.SelectedValue);
Upvotes: 4