3

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?

7
  • Have you tried nested repeater control? Commented Apr 11, 2013 at 10:30
  • I don`t think that nested listview is a problem. I will try it now but I think that the DataSource like List<Item> itemList list is not the right way ... I will try with repeater and give here a comment about it Commented Apr 11, 2013 at 10:32
  • Like I said, the same error - DataBinding: MyRepository ... datasource GRoupOfItems does not contains a property with name itemList ... It bind groupName property like it should but cant find itemlist like property because it is not property... Commented Apr 11, 2013 at 10:34
  • 1
    Please try to make GroupOfItems::itemList a property: public List<Item> ItemList { get; private set; }. Commented Apr 11, 2013 at 10:37
  • Hehe I cant belive it that I missed this :P This works for my example, so post it like an answer... I cant belive it that I didn`t see this :P Commented Apr 11, 2013 at 10:42

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.