您可以使用嵌套Repeater
的 LINQGroupBy
方法来实现此目的。
我不确定您的 DataSource 以及您如何绑定cdcatalog
中继器,因此在此示例中,我使用的是CatalogItem
s 列表。这是CatalogItem
课程:
public class CatalogItem
{
public string Cost_Code { get; set; }
public int Total { get; set; }
public decimal Price { get; set; }
}
您将需要一个页面级列表:
List<CatalogItem> items;
基本上,您会将外部中继器绑定到按 . 分组的列表Cost_Code
。然后,内部中继器将绑定到CatalogItem
s 的过滤列表。像这样:
protected void Page_Load(object sender, EventArgs e)
{
items = new List<CatalogItem>();
items.Add(new CatalogItem() { Cost_Code = "ENE-Direct", Total = 33, Price = 196 });
items.Add(new CatalogItem() { Cost_Code = "ENE-Direct", Total = 8, Price = 96 });
items.Add(new CatalogItem() { Cost_Code = "ENE-Direct", Total = 15, Price = 1260 });
items.Add(new CatalogItem() { Cost_Code = "ENE-Direct", Total = 10, Price = 228 });
items.Add(new CatalogItem() { Cost_Code = "ENE-Direct", Total = 125, Price = 60 });
items.Add(new CatalogItem() { Cost_Code = "IND038301", Total = 10, Price = 258 });
items.Add(new CatalogItem() { Cost_Code = "IND038302", Total = 20, Price = 358 });
items.Add(new CatalogItem() { Cost_Code = "IND038303", Total = 30, Price = 458 });
items.Add(new CatalogItem() { Cost_Code = "IND038304", Total = 40, Price = 558 });
this.cdcatalog.DataSource = items.GroupBy(c => c.Cost_Code).Select(c => new CatalogItem() { Cost_Code = c.Key });
this.cdcatalog.DataBind();
}
protected void cdcatalog_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptItems = (Repeater)e.Item.FindControl("rptItems");
CatalogItem catalogGroup = (CatalogItem)e.Item.DataItem;
rptItems.DataSource = items.Where(i => i.Cost_Code == catalogGroup.Cost_Code);
rptItems.DataBind();
}
}
ascx 代码如下所示:
<asp:Repeater ID="cdcatalog" runat="server" OnItemDataBound="cdcatalog_ItemDataBound">
<HeaderTemplate>
<table border="1" width="500">
<tr>
<th>Cost Code</th>
<th>Total</th>
<th>Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("Cost_Code")%> </td>
</tr>
<asp:Repeater ID="rptItems" runat="server">
<ItemTemplate>
<tr>
<td></td>
<td><%#Eval("Total")%> </td>
<td><%#Eval("Price")%> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
最终输出将如下所示:
