繁体   English   中英

asp:repeater显示每个类别中的项目数

[英]asp:repeater show count of items in each category

我有一个简单的转发器,可获取“小部件”的“组”。主页列出了所有组:

<ItemTemplate>
    <tr>
        <td width="60" class="center"><%# DataBinder.Eval(Container.DataItem, "Number") %></td>
        <td><a href="Stories.aspx?ProjectID=<%# DataBinder.Eval(Container.DataItem, "ProjectId") %>"><%# DataBinder.Eval(Container.DataItem, "Name") %></a></td>
        <td><%# DataBinder.Eval(Container.DataItem, "Description") %></td>
    </tr>
</ItemTemplate>

背后的代码

private void LoadForm()
{
    using (MarketingWebContentEntities context = new MarketingWebContentEntities())
    {
        rptGroup.DataSource = (from groups in context.URLGroup
                               select groups).ToList();
        rptGroup.DataBind();
    }
}

我想在中继器中显示每个“组”中“小部件”的数量。 我知道我需要在“小工具”表上运行查询,以查看该列表中有多少个项目。 我只是不确定如何在转发器标记中添加它。

如评论中所述,您可以为此使用ItemDataBound事件。

这个例子在VB中-自从我写C#以来已有一段时间了,尽管会给你一个想法。 我也没有检查它的语法,它只是使您开始运行的一个示例。

在您的<ItemTemplate>添加您自己,例如ASP:Label 在这种情况下,它称为myLabel

因此,在后面的代码中,创建一个私有方法来处理ItemDataBound事件。

Protected Sub myRepeater_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles myRepeater.ItemDataBound

    If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then

        Dim uG As URLGroup = CType(e.Item.DataItem, URLGroup)
        '' you now have the group for that one item
        '' you should now be able to get additional information needed.
        '' you can also get the myLabel from this item 
        dim lbl as Label = CType(e.Item.FindControl("myLabel", Label)
        '' and set its text to whatever you need
        lbl.Text = MyCounter

    End If

End Sub

希望这会帮助您。

这也是它的MSDN文档的链接。

我使用了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();


        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM