关于Repeater嵌套绑定的问题

时间:2022-06-01 21:21:34

前台代码:

<div id="firstpane" class="menu_list">
                <asp:Repeater ID="rep1" runat="server" onitemdatabound="rep1_ItemDataBound">
                    <ItemTemplate>
                        <p class="menu_head" id="p<%# Eval("p_typeid")%>"><%#Eval("type_name") %></p>
                        <div class="menu_body" id="div<%# Eval("p_typeid")%>">
                           
                            <asp:Repeater ID="rep2" runat="server">
                                <ItemTemplate>
                                    <a id="a<%# Eval("p_typeid")%>" href="media.aspx?types=<%# Eval("p_typeid")%>&ptypeid=<%# Eval("p_typepid") %>">
                                        <%# Eval("type_name")%></a>
                                </ItemTemplate>
                            </asp:Repeater>
                              
                        </div>
                    </ItemTemplate>
                </asp:Repeater>
            </div>

后台代码:

 protected void rep1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //判断里层repeater处于外层repeater的哪个位置( AlternatingItemTemplate(该模板定义如何显示控件中的交替项),FooterTemplate(脚模板),

        //HeaderTemplate(头模板),ItemTemplate(项模板),SeparatorTemplate(分割模板,元素能够用于描述每个记录之间的分隔符))

        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if (e.Item.FindControl("rep2") != null)
            {
                DataRowView dv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项

                string pid = dv.Row["p_typeid"].ToString();//获取填充子类的id

                Repeater rp = (Repeater)e.Item.FindControl("rep2");//找到里层的repeater对象

                rp.DataSource = product.getP_typeById(Convert.ToInt32(pid), OleDbHelp.ConnCn);
                rp.DataBind();
            }
        }
    }