Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

asp:DropDownList Binding to List

0.00/5 (No votes)
29 May 2014 1  
DropDownList requires the binding object to have a property

If you wonder why your <asp:DropDownList> is throwing a binding error when you try to get it to display a List<> of custom objects, it might be that your object has a variable, instead of a property.
So, this:

List<NameValue> listCategories = csNewsArticleProcess.GetNewsPublicCategories();
ddlCategory.DataValueField = "Value";
ddlCategory.DataTextField = "Name";
ddlCategory.DataSource = listCategories;
ddlCategory.DataBind(); 

won't work if the NameValue is declared as:

public class NameValue
{
public string Name;
public string Value;
}

Instead it needs to be:

public class NameValue
{
public string Name { get; set;}
public string Value{ get; set;}
} 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here