0

「ウィジェット」の「グループ」を取得する単純なリピーターがあります。ホームページには、すべてのグループが一覧表示されます。

<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();
    }
}

リピーター内で、各「グループ」内の「ウィジェット」の数を表示したいと思います。'widgets'テーブルでクエリを実行して、そのリストに含まれるアイテムの数を確認する必要があることはわかっています。リピーターのマークアップ内にそれを追加する方法がわかりません。

4

2 に答える 2

2

コメントで述べたように、これには ItemDataBound イベントを使用できます。

この例は VB です。C# を書いてからしばらく経ちましたが、アイデアが得られるでしょう。構文もチェックしていません。これは、起動して実行するための例です。

あなた<ItemTemplate>自身を追加して、 a 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

うまくいけば、これであなたの道が開けるでしょう。

こちらもMSD​​Nドキュメントへのリンクです。

于 2012-11-07T20:40:24.000 に答える
1

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();


        }
    }
}
于 2012-11-12T15:50:08.637 に答える