Accessing parent data in nested repeater, in the HeaderTemplate

asp.netNested Repeater

asp.net Problem Overview


Simple question, not sure there's a simple answer!

So here's the code: (I've simplified it a lot to make it easier to read)

<asp:Repeater runat="server>
    <ItemTemplate>
        <asp:Repeater runat="server">
            <HeaderTemplate>
                <h1>My header here for: <%# OuterContainer.DataItem.MyItemName %> </h1>
            </HeaderTemplate>
            <ItemTemplate>
                My items code here
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

How, in the HeaderTemplate - can I access the DataItem in the parent repeater?

asp.net Solutions


Solution 1 - asp.net

I have found the answer actually:

Use:

<HeaderTemplate>
    <%# ((RepeaterItem)Container.Parent.Parent).DataItem %>
</HeaderTemplate>

Solution 2 - asp.net

Solution given by Paul didn't work for me, but this did:

<%# DataBinder.Eval(Container.Parent.Parent, "DataItem.YourProperty")%> 

Solution 3 - asp.net

This is an old thread, but it seems proper to add:

In my case I have 2 nested ASPxGridView controls (DevExpress) and Container.Parent.Parent didn't work.

To access parent's data item from child, this is what worked for me:

<%# DataBinder.Eval(Container.NamingContainer.NamingContainer, "DataItem.DbField")%>

Solution 4 - asp.net

If I want to retrieve a property of a parent repeater I typically do this:

<%# DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "ThePropertyName")%>

Solution 5 - asp.net

I have used as below. Two Repeaters act as Parent and Child.below how I get Parent value of ID Column inside Child repeater.

<%# DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "ID") %>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaulView Question on Stackoverflow
Solution 1 - asp.netPaulView Answer on Stackoverflow
Solution 2 - asp.netgreenoldmanView Answer on Stackoverflow
Solution 3 - asp.netbobetkoView Answer on Stackoverflow
Solution 4 - asp.netChad KuehnView Answer on Stackoverflow
Solution 5 - asp.netmzonerzView Answer on Stackoverflow