58

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?

Paul
  • 8,821
  • 11
  • 56
  • 103

5 Answers5

93

I have found the answer actually:

Use:

<HeaderTemplate>
    <%# ((RepeaterItem)Container.Parent.Parent).DataItem %>
</HeaderTemplate>
Paul
  • 8,821
  • 11
  • 56
  • 103
  • 5
    and if you're in code-behind in the `ItemDataBound` method: `((RepeaterItem)e.Item.Parent.Parent).DataItem` – drzaus Feb 04 '13 at 20:07
  • 11
    Instead of `.Parent.Parent`(which can be incorrect) use `.NamingContainer`. – Tim Schmelter Feb 28 '13 at 13:45
  • 3
    But I am getting this Error:[InvalidCastException] Unable to cast object of type 'System.Web.UI.WebControls.Repeater' to type 'System.Web.UI.WebControls.RepeaterItem'. – sms Jan 16 '14 at 13:07
  • 1
    It should be cast to a Repeater, not a RepeaterItem: `((Repeater)e.Item.NamingContainer.NamingContainer).DataItem` – Thomas Higginbotham Mar 05 '14 at 18:58
41

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

<%# DataBinder.Eval(Container.Parent.Parent, "DataItem.YourProperty")%> 
greenoldman
  • 15,621
  • 22
  • 103
  • 174
  • 1
    Can you tell me why `Container.Parent.Parent` instead of `Container.Parent`? – SamuraiJack Jul 27 '13 at 17:24
  • @Arbaaz this will depend on how many "parents" ie levels you have. eg parent of parent. Its quite similar in logic to the way jquery aproaches it if you know what i mean – JazziJeff Feb 17 '14 at 16:30
  • Using the NamingContainer hierarchy is better. The NamingContainer hierarchy can be understood from the markup, while the Parent hierarchy may contain extra controls such as the Table generated by the FormView. – Sarsaparilla Mar 12 '15 at 16:37
  • 7
    Parent is ItemTemplate, Parent.Parent is Repeater –  Sep 11 '16 at 20:29
9

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")%>
bobetko
  • 4,666
  • 13
  • 56
  • 81
4

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

<%# DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "ThePropertyName")%>
Chad Kuehn
  • 3,683
  • 30
  • 29
0

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") %>
mzonerz
  • 1,070
  • 11
  • 19