c# - Retrieving selected enum from dropdown list -


i binding enumeration dropdown list in asp.net 4.0 c#

enumernation is:

public enum frequency {     [description("select frequency")]     none,      [description("every hour/mintues")]     everyhourormintues,       [description("previous day data")]     previousdaydata,      [description("once week")]     onceaweek } 

on selection of value dropdown want enum value in return: doing like:

frequency selectedfrequency; foreach (frequency f in enum.getvalues(typeof(frequency))) {     if (f.tostring().equals(this.dropdownlistfrequency.selectedvalue))     {         selectedfrequency = f;         break;     } } 

it working poor way guess, looping through each of items in enum (even though enum small)

how can retrieve selected enum like:

frequency selectedvalue = enum.getvalues(typeof(frequency)).cast<frequency>().select(f => f.tostring().equals(this.dropdownlistfrequency.selectedvalue)); 

i understand above given code has casting issue.

edit more information, here how binding enum 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() extension method returns value of description attribute of enum

if value of dropdown list enum's integer representation (e.g. 0,1,2...), can cast enum:

frequency f = (frequency)int.parse(dropdownlistfrequency.selectedvalue); 

if value of dropdown list enum's string representation (e.g. "none", "everyhourormintues"...), can use enum.parse():

frequency f = (frequency)enum.parse(     typeof(frequency), dropdownlistfrequency.selectedvalue); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -