我做了两节课-仅针对此问题的示例:
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 文件中,我拥有ObjectDataSource
并且ListView
喜欢:
<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
像 a那样绑定DataSource
到嵌套ListView
的 ( DataSource='<%# Eval("itemList") %>'
) (因为itemList
它不是类中的属性)。这是正确的绑定方法还是我需要改变我的方法?