2013-04-11 68 views
3

我做了两个类显示类嵌套列表 - 例如只有一个问题:如何在列表视图

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

而在我的.aspx文件中我有ObjectDataSourceListView,如:

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

这是这个问题的一个非常快速的例子。 Eveything正常工作,但我不能将itemList绑定为DataSource嵌套ListViewDataSource='<%# Eval("itemList") %>')(因为itemList不是类中的属性)。这是绑定的正确方法,还是我需要改变我的方法?

+0

你有没有试过嵌套中继器控制? – Yahya 2013-04-11 10:30:28

+0

我不认为嵌套的listview是一个问题。我会尝试它,但我认为DataSource像列表 itemList列表是不正确的...我会尝试与中继器,并在这里给它一个评论 – janilemy 2013-04-11 10:32:51

+0

就像我说的,同样的错误 - DataBinding:MyRepository .. 。datasource GRoupOfItems不包含名为itemList的属性...它绑定groupName属性就像它应该但无法找到itemlist属性,因为它不属性... – janilemy 2013-04-11 10:34:57

回答

2

listItems构件被定义为字段,而不是作为属性,防止反射找到它使用GetProperties(这是什么数据绑定实际执行)。

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

实际上,您应该总是使用属性来公开您的数据接口。这就是encapsulation的含义。