Friday, 15 May 2015

c# - Getting Id of enum which I have bound to a dropdown list -



c# - Getting Id of enum which I have bound to a dropdown list -

i have set of enums bound dropdown list.

((dropdownlist)control).datasource = defaultsync; ((dropdownlist)control).databind();

here defaultsync list contains 2 enums.

list<myenum> defaultsync=(list<syncrequesttypeenum>)(enum.getvalues(typeof(syncrequesttypeenum)).cast<syncrequesttypeenum>().except(new syncrequesttypeenum[] { syncrequesttypeenum.projectlevel })).tolist();

now wanted id of enum based on user selection of dropdownlist. used next code giving error list not contain value it.

public int enumid { { homecoming convert.toint32(ddlselection.selectedvalue); } set { ddlselection.selectedvalue = convert.tostring(value); } }

can help on this?

error : 'ddlselection has selectedvalue invalid because not exist in list of items. parameter name: value

in order utilize selectedvalue property need specify property of info item value property , 1 displayed text. suggest changing code this:

var list = command dropdownlist; list.datasource = enum.getvalues(typeof(syncrequesttypeenum)) .cast<syncrequesttypeenum>() .except(/*..*/) .select(x => new keyvaluepair<syncrequesttypeenum, string>(x, x.tostring()) .tolist(); list.datavaluefield = "key"; list.datatextfield = "value"; list.databind();

and property should work fine.

another illustration , more detailed explanation on msdn.

c#

No comments:

Post a Comment