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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I have found the answer actually:
Use:
<HeaderTemplate>
<%# ((RepeaterItem)Container.Parent.Parent).DataItem %>
</HeaderTemplate>
Method 2
Solution given by Paul didn’t work for me, but this did:
<%# DataBinder.Eval(Container.Parent.Parent, "DataItem.YourProperty")%>
Method 3
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")%>
Method 4
If I want to retrieve a property of a parent repeater I typically do this:
<%# DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "ThePropertyName")%>
Method 5
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") %>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0