Binding a DataList to an ArrayList of objects

  • Thread starter Thread starter Stefan Landgraf
  • Start date Start date
S

Stefan Landgraf

I try binding a DataList to an ArrayList of objects but keep getting the error:

DataBinder.Eval: 'espod.myClass' does not contain a property with the name Name

Heres the code:

<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"Name") %>
</ItemTemplate>
</asp:DataList>


class myClass
{
public string Name;

public myClass(string name)
{
Name = name;
}
}

public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;

private void Page_Load(object sender, System.EventArgs e)
{
ArrayList al = new ArrayList();
al.Add(new myClass("Peter"));
al.Add(new myClass("Mary"));

DataList1.DataSource = al;
DataList1.DataBind();
}
}

Does anyone know whats wrong here?

Stefan
 
Define Property Name, cause you can not bind to Field only to property:

class myClass
{
private string _Name;

public string Name
{
get
{
return _Name;
}
}
}
 
Hi,

did you try by creating the Name as property into the custom class?

class myClass
{
public string _name;

public myClass(string name)
{
_name = name;
}

public string Name
{
get
{
return _name;
}
}
}
 
Back
Top