Repeater in Repeater

狂风中的少年 提交于 2019-11-26 20:16:06

In the parent repeater, attach a method to the OnItemDataBound event and in the method, find the nested repeater and data bind it.

Example (.aspx):

<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ItemBound">
    <ItemTemplate>
        <!-- Repeated data -->
        <asp:Repeater ID="ChildRepeater" runat="server">
            <ItemTemplate>
                <!-- Nested repeated data -->
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

Example (.cs):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ParentRepeater.DataSource = ...;
        ParentRepeater.DataBind();
    }
}

protected void ItemBound(object sender, RepeaterItemEventArgs args)
{
    if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater");
        childRepeater.DataSource = ...;
        childRepeater.DataBind();
    }
}

I would add a DataBinding event to the child repeater itself:

<asp:Repeater ID="parentRepeater" runat="server">
    <asp:Repeater ID="childRepeater" runat="server"
        OnDataBinding="childRepeater_DataBinding" />
</asp:Repeater>

Then just implement it:

protected void childRepeater_DataBinding(object sender, System.EventArgs e)
{
    Repeater rep = (Repeater)(sender);

    int someIdFromParentDataSource = (int)(Eval("ParentID"));

    // Assuming you have a function call `GetSomeData` that will return
    // the data you want to bind to your child repeater.
    rep.DataSource = GetSomeData(int);
    rep.DataBind();
}

I prefer to do it at the control level instead of the ItemDataBound level so that if you ever have to remove controls or items within your templates you don't have to worry about looking for code in the parent controls that use it. It get's all localize witht he control itself. Plus you never have to do a FindControl.

If you want to replace a control in the future you can just delete it and your code will still work since it is all self contained. Using the ItemDataBound would cause your code to still compile but crash or act unexpectedly at runtime because of it's reliance on child controls.

bill

Here is how it's done:

DataSource='<%# ((System.Data.DataRowView)Container.DataItem)[3] %>'

So if you know the column in the parent table that holds the child table/datasource for the nested repeater you can put this directly in the aspx file.

If I need to do that, I usually do it using the ItemDataBound event of the parent repeater to bind the child repeater. If e is your EventArgs parameter, you'll have access to the child repeater via e.Item.FindControl(), and access to the data via e.Item.DataItem.

Ravi Ram

Repeater1 OnItemDataBound event, then FindControl Repeater2. The code-behind will not find the nested Repeater2! You have to use FindControl("Repeater2").

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.DataItem != null)
    {
        MemberView dataRow = (MemberView)e.Item.DataItem;
        var cat = MemberPresenter.getMemberID(dataRow.memID);

        Repeater rp2 = (Repeater)e.Item.FindControl("Repeater2");
        rp2.DataSource = cat;
        rp2.DataBind();
    }  
}
NiK

Here is an example of how to do this: Article for nested repeater control

 protected void MainRepeater_ItemDataBound(object sender, RepeaterItemEventArgs args)
    {
         if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater");

                DataTable innerTable= ((DataRowView)args.Item.DataItem)["InnerTableColumnName"] as DataTable;
                childRepeater.DataSource = tasksDetails;
                childRepeater.DataBind();
            }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!