Load DropDownList

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have a list of entities (with properties Key and Description).

Which is the best manner of load dropdownlist with this list ?

Thanks in advance
 
The best manner is to databind the ddl to the list like this:

myDdl.DataSource = myList;
myDdl.DataTextField = "Description";
myDdl.DataValueField = "Key";
myDdl.DataBind();

You can do the same in the markup.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
Assuming you have the list in the form of array/datatable/arraylist:

DropDown.DataSource = <entity collection>;
DropDown.DataTextField = <entity property name for text display
(description)>;
DropDown.DataValueField = <entity property name for value (key)>;
DropDown.DataBind ();
 
I'd try but it appears the following error:

Data source is an invalid type. It must be either an IListSource,
IEnumerable, or IDataSource.

My List of entities is a Dictionary<int, MyEntityObject>.
 
Assign DataSource property to Dictionary.Values, which has the desired
collection having your entities.
 
Back
Top