Melursus
Melursus

Reputation: 10608

Asp.Net - DropDownList.DataValueField in a IList

In Asp.Net 3.5, it is possible that the DataValueField is a IList ?

myddl.DataValueField = "ListOfId"

where ListOfId as a IList in my DataSource

Upvotes: 1

Views: 6333

Answers (2)

John R6
John R6

Reputation: 21

You have to pass the string name of the field you are assigning to the DataTextField and DataValueField before you DataBind() the DropDownList.

Example:

protected void populateDropDownList(){
   IList<MyObject> myObjectList = getMyObjectList();

   DropDownList1.DataSource = myObjectList;
   DropDownList1.DataTextField = "FullName"; // exact name of field in class
   DropDownList.DataValueField = "id";

   DropDownList.DataBind();

}

Upvotes: 2

mamoo
mamoo

Reputation: 8166

It's not possible. DataValueField must be a string, that is the name of a public property of the binded datasource:

http://msdn.microsoft.com/en-en/library/system.web.ui.webcontrols.listcontrol.datavaluefield(VS.90).aspx

If you have a simple list of string you can just bind it to the dropdown:

C# - Dumping a list to a dropdownlist

Upvotes: 0

Related Questions