簡體   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