janilemy
janilemy

Reputation: 565

How to show nested list in Class in listview

I have made two classes - example for this question only:

public class Item
{
    public int itemId { get; set; }
    public string name { get; set; }

    public Item() { }

    public Item(int itemId, string name) 
    {
        this.itemId = itemId;
        this.name = name;
    }
}

public class GroupOfitems
{
    public string groupName { get; set; }
    public List<Item> itemList;

    public GroupOfitems()
    {
        itemList = new List<Item>();
    }

    public GroupOfitems(string groupName, List<Item> itemList)
    {
        this.groupName = groupName;
        this.itemList = itemList;
    }
}

public List<GroupOfitems> TestMethod()
{
    // ADD SOME ITEMS TO FRUIT GROUP
    List<Item> bla = new List<Item>();
    bla.Add(new Item(1, "Banana"));
    bla.Add(new Item(2, "Apple"));

    // ADD SOME ITEMS TO VEGETABLES GROUP
    List<Item> bla2 = new List<Item>();
    bla2.Add(new Item(5, "Carot"));
    bla2.Add(new Item(6, "Tomato"));

    // ADD SOME ITEMS TO SOURCE
    List<GroupOfitems> result = new List<GroupOfitems>();
    result.Add(new GroupOfitems("Fruit", bla));
    result.Add(new GroupOfitems("Vegetables", bla2));

    // RETURN SOURCE
    return result;  
}

And in my .aspx file I have ObjectDataSource and ListView like:

<asp:ObjectDataSource ID="odsRecipesIngredientListTest" runat="server"
    SelectMethod="TestMethod" TypeName="MyRepository">            
</asp:ObjectDataSource>

<asp:ListView ID="lvRecipeIngredients" runat="server" 
    DataSourceID="odsRecipesIngredientListTest" 
    ItemPlaceholderID="itemPlaceHolderId">
    <LayoutTemplate>
        <asp:PlaceHolder id="itemPlaceHolderId" runat="server" />
    </LayoutTemplate>
    <ItemTemplate>
        <asp:Label ID="lblGroupName" Text='<%# Eval("groupName") %>' runat="server" /><br />

        <asp:ListView ID="lvIngredientList" runat="server" 
            DataSource='<%# Eval("itemList") %>' 
            ItemPlaceholderID="itemPlaceHolderId">
            <LayoutTemplate>
                <asp:PlaceHolder id="itemPlaceHolderId" runat="server" />
            </LayoutTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" Text='<%# Eval("itemId") %>' runat="server" />
                <asp:Label ID="Label16" Text='<%# Eval("name") %>' runat="server" />
            </ItemTemplate>
        </asp:ListView>
    </ItemTemplate>
</asp:ListView>

This is a really quick example for this question. Eveything works fine but I just can't bind itemList like a DataSource to nested ListView like (DataSource='<%# Eval("itemList") %>') (because itemList is not a property in class). Is this the right approach for binding or do I need to change my approach?

Upvotes: 3

Views: 648

Answers (1)

Carsten
Carsten

Reputation: 11616

The listItems member is defined as field, not as property, preventing the reflection to find it using GetProperties (which is, what data binding actually does).

public class GroupOfitems
{
    public string groupName { get; private set; }

    public List<Item> ItemList { get; private set; }

    public GroupOfitems()
    {
        this.ItemList = new List<Item>();
    }

    public GroupOfitems(string groupName, List<Item> itemList)
    {
        this.groupName = groupName;
        this.ItemList = itemList;
    }
}

In fact you should always use properties to expose your data interface. That's what meant by encapsulation.

Upvotes: 2

Related Questions