如果只关心在集合中显示数据,则可以添加另一个静态方法,该方法返回对象readonlycollection并绑定到该对象。或者,您可以丢弃objectdatasource并在代码中执行它,例如:
var myObj = ClassName.StaticMethod(someParam);
MyRepeater.DataSource = myObj.Collection;
MyRepeater.DataBind();
如果需要在类和集合中同时显示数据,则可以向ItemTemplate添加另一个中继器:
<asp:objectdatasource runat="server" id="myData"
selectmethod="StaticMethod" typename="ClassName">
<selectparameters>
<asp:parameter name="ParamName" defaultvalue="Value" />
</selectparameters>
</asp:objectdatasource>
<asp:Repeater runat="server" datasourceid="myData" OnItemDataBound="Rep_ItemDataBound">
<ItemTemplate>
<%# Eval("SomeProperty") %>
<ul>
<asp:Repeater id="RepCollection" runat="server">
<ItemTemplate>
<li><%# Eval("SomeCollectionProperty") %></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</ItemTemplate>
</asp:Repeater>
itemdatabound方法类似于:
protected void Rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
var dataItem = (ClassName)e.Item.DataItem;
var innerRepeater = (Repeater)e.Item.FindControl("RepCollection");
innerRepeater.DataSource = dataItem.Collection;
innerRepeater.DataBind();
}
}