OnItemDataBound不会在嵌套的Repeater上触发?

时间:2013-02-27 12:50:53

标签: c# asp.net

我在另一个Repeater的ItemTemplate中有一个嵌套的Repeater。 我将一个OnItemDataBound函数添加到转发器但代码不执行

aspx

<asp:Repeater ID="rptComponents" runat="server" OnItemDataBound="rptComponents_ItemDataBound">
            <HeaderTemplate>
            </HeaderTemplate>
            <ItemTemplate>
                <div class="row-fluid">
                    <div class="span2">
                        <asp:Repeater ID="rptComponentsImages" runat="server" OnItemDataBound="rptComponentsImages_ItemDataBound">
                            <HeaderTemplate>
                                <ul class="bxslider">
                            </HeaderTemplate>
                            <ItemTemplate>
                                <li>
                                    <img src="<%# Eval("CKCI_File")%>" /></li>
                            </ItemTemplate>
                            <FooterTemplate>
                                <li runat="server" id="liNoData" visible="false">
                                    <img src="http://placehold.it/132&text=No image" alt=""></li>
                                </ul>
                            </FooterTemplate>
                        </asp:Repeater>
                    </div>
                    <div class="">
                        <%# Eval("CKC_Title") %>
                    </div>


                </div>
            </ItemTemplate>
            <FooterTemplate>
            </FooterTemplate>
        </asp:Repeater>

代码

protected void rptComponents_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            ClutchKitComponent rd = (ClutchKitComponent)e.Item.DataItem;

            List<ClutchComponentImage> kitComponentImages = (List<ClutchComponentImage>)rd.images;

            Repeater rt = (Repeater)e.Item.FindControl("rptComponentsImages");
            rt.DataSource = kitComponentImages;
            rt.DataBind();

        }
    }

    protected void rptComponentsImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Repeater rt = (Repeater)sender;
        if (rt.Items.Count < 1)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                e.Item.FindControl("liNoData").Visible = true;
            }
        }
    }

第二个功能不执行

2 个答案:

答案 0 :(得分:1)

我在其中一个列表上有一个null,所以它抛出一个我没有抓到的ex。 即使没有数据,也会触发OnItemDataBound。

答案 1 :(得分:0)

你有里面转发器的ItemDataBound事件吗? 你是否尝试在ItemCreated或第一个ItemDataBound事件中添加该事件?

rt.ItemDataBound += new RepeaterItemEventHandler(rt_ItemDataBound);

检查嵌套转发器的此链接。 http://www.codeproject.com/Articles/20230/Nesting-Data-bound-Repeater-Controls-in-ASP-Net