我使用了 OnItemDataBount 事件
<asp:Repeater runat="server" ID="rptGroup" OnItemDataBound="rptDestinationCount_ItemDataBound">
<HeaderTemplate>
<table id="tblUrlGroup" class="table table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Name</th>
<th style="width:20px;">Count</th>
<th style="width:35px;">Add</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><a href="ManageGroup.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "URLGroupRowID") %>"><i class="icon-wrench" rel="tooltip" title="Edit Group Name"></i></a> <a href="DestinationGroup.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "URLGroupRowID") %>"><%# DataBinder.Eval(Container.DataItem, "Name") %></a></td>
<td class="center"><asp:HiddenField runat="server" ID="hidURLGroupRowID" Value='<%# DataBinder.Eval(Container.DataItem, "URLGroupRowID") %>' /><asp:Label runat="server" ID="lblCount"></asp:Label></td>
<td class="center">
<a href="DestinationGroup.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "URLGroupRowID") %>"><i class="icon-plus" rel="tooltip" title="Manage Destination URLs"></i></a>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
在功能上,我确保我只查看中继器项目和项目模板。隐藏字段使用数据源的 ID 设置。这允许我运行查询并将 lblCount.Text 设置为目标计数。
代码背后
protected void rptDestinationCount_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
using (MarketingWebContentEntities context = new MarketingWebContentEntities())
{
Label lblCount = (Label)e.Item.FindControl("lblCount");
HiddenField hidURLGroupRowID = (HiddenField)e.Item.FindControl("hidURLGroupRowID");
int groupRowID = Convert.ToInt32(hidURLGroupRowID.Value);
var destination = (from dest in context.URLDestination
where dest.URLGroup.URLGroupRowID == groupRowID
select dest).ToList();
lblCount.Text = destination.Count.ToString();
}
}
}