Binding a DataList to an ArrayList of objects

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
 
A

Anatoly

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

class myClass
{
private string _Name;

public string Name
{
get
{
return _Name;
}
}
}
 
T

Teemu Keiski

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;
}
}
}
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top