Hello everyone I’ve been trying to put some objects in an ASP.NET list box but it’s just not working.
I do have an overridden ToString method so I can’t understand why this statement won’t work.
Here’s the code that I use:
for (int i = 0; i < fitnessClassList.Count(); i++)
{
lbDisplayItems.Items.Add(fClassList.getFClass(i));
}
And the errors that I get:
Error 2 Argument 1: cannot convert from
'FitClassManage' to
'System.Web.UI.WebControls.ListItem'
Error 1 The best overloaded method match for
'System.Web.UI.WebControls.ListItemCollection.Add(System.Web.UI.WebControls.ListItem)'
has some invalid arguments
The ASP.NET ListBox is not like the WinForms ListBox. You cannot add any object to him. Its item collection (ListItemCollection) is limited to ListItem (so you can’t add your business objects relying on
ToString()for visualization).Use this code:
Do not forget that if you’ll use that
ListItemyou won’t have the object but its display name (the result ofToString()). See the link about theListItemfor more details.As alternative you may set the DataSource of the ListView to your
fitnessClassList(if it supports that in any way, see this overview on MSDN).