Amandeep Singh
Amandeep Singh

Reputation: 3840

Unable to Bind List of objects to Listview in Asp.net

I am trying to bind listview with list of user defined objects. I am getting the error as ItemClass' does not contain a property with the name 'Name'

Below is my Asp.net code :

 <asp:ListView ID ="listView" runat="server">
        <ItemTemplate>
        <asp:Label runat="server" Text='<%#Eval("Name")%>'>

        </asp:Label>
        </ItemTemplate>



    </asp:ListView>

I am trying to bind using the c# code :

 var list = new List<ItemClass>();

            ItemClass item1 = new ItemClass();
            item1.Name = "Aman";

            ItemClass item2 = new ItemClass();
            item2.Name = "Arjit";

            list.Add(item1);
            list.Add(item2);

            listView.DataSource = list;
            listView.DataBind();

Can you please help me resolve this issue.

Thanks

Amandeep

Upvotes: 3

Views: 3364

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34834

Verify that Name is a public property in the class ItemClass, like this:

public class ItemClass
{
    public string Name { get; set; }
}

Note: Eval() will only work on public properties.

Upvotes: 2

Related Questions