Load DropDownList

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
 
E

Eliyahu Goldin

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
 
G

Guest

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 ();
 
G

Guest

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>.
 
G

Guest

Assign DataSource property to Dictionary.Values, which has the desired
collection having your entities.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top