Accessing Parent DataItem of a Child DataItem

We've all iterated through some kind of repeater (datalist, datagrid, an ACTUAL repeater) and used the DataBinder to display a property of the object bound to the current DataItem without wasting time writing an ItemDataBound event handler:

<asp:Repeater>
   <ItemTemplate>
      <%#DataBinder.Eval(Container.DataItem, "PropertyName")%>
   </ItemTemplate>
</asp:Repeater>

But have you ever been iterating through a Repeater WITHIN a Repeater and wondered: "gee, how do I access the outer DataItem for display within the inner DataItem's template without writing a complicated ItemDataBound event handler?"  I.e.

<asp:Repeater ID="_outerRepeater">
   <ItemTemplate>
      <%#DataBinder.Eval(Container.DataItem, "PropertyName")%>
      <asp:Repeater ID="_innerRepeater">
         <ItemTemplate>
            [Outer Repeater DataItem]
            <%#DataBinder.Eval(Container.DataItem, "PropertyName")%>
         </ItemTemplate>
      </asp:Repeater>
   </ItemTemplate>
</asp:Repeater>

Well, I wondered just that the other day.  I asked around to see if there was a shortcut anybody around inetium knew, to no avail.  Seems everyone thought it should be easy, but they had never thought to attempt it before.  Googling was a bit challenging for this task (what would you put in the search box?), so I begain tinkering in code, using asp.net 2.0 in Visual Studio 2005 for increased intellisense and figured out this solution:

<asp:Repeater ID="_outerRepeater">
   <ItemTemplate>
      <%#DataBinder.Eval(Container.DataItem, "PropertyName")%>
      <asp:Repeater ID="_innerRepeater">
         <ItemTemplate>
<%#((System.Web.UI.WebControls.RepeaterItem)Container.Parent.Parent).DataItem%>
            <%#DataBinder.Eval(Container.DataItem, "PropertyName")%>
         </ItemTemplate>
      </asp:Repeater>
   </ItemTemplate>
</asp:Repeater>

The container is what holds the DataItem (in an event handlers, that'd be e.Item I'd guess) and it's parent is the inner repeater.  So you'll need the inner repeater's parent, the outer repeater's DataItem.  Seems to call the ToString() method of it, so it's limited, but that's exactly what I wanted (I was binding the outer repeater do directories and the inner repeater to files inside of that directory, so the point was to get a link to [Directory Name]/[File Name].

Published Friday, December 01, 2006 10:18 AM by vbullinger

Comments

# re: Accessing Parent DataItem of a Child DataItem

Hello -

I could not get this to work.May be my situation is different.

<asp:Repeater ID="IssueSummary_Repeater" runat="server"  >
                   <HeaderTemplate>
                       <table  border="0" cellspacing="1" cellpadding="0">
                        <tr>
                           <td> Internal Message</td>
                           <td> External Message</td>
                           <td> State</td>
                        </tr>
                       
                   </HeaderTemplate>
                   <ItemTemplate>
                       <fieldset>
                            <tr>
                               <td><asp:Literal ID="litInternalMessage" runat="server"  Text='<%# DataBinder.Eval(Container.DataItem, "[\"sInternalMessage\"]")%>'></asp:Literal></td>
                               <td><asp:Literal ID="litExternalMessage" runat="server"  Text='<%# DataBinder.Eval(Container.DataItem, "[\"sExternalMessage\"]")%>'></asp:Literal></td>
               <td><select
                                       onchange="ChangeIssueState(this,'<%# DataBinder.Eval(Container.DataItem, "[\"fkUserOrder\"]")%>','<%# DataBinder.Eval(Container.DataItem, "[\"pkIssueTracker\"]")%>');">
                                       <asp:Repeater ID="IssueState_Repeater" OnLoad="OnLoad_BindIssueStateList" runat="server">
                                           <ItemTemplate>
                                           
                                           <option  
                                               fieldTypeName='<%# DataBinder.Eval(Container.DataItem, "[\"sIssueState\"]")%>'
                                               value='<%# DataBinder.Eval(Container.DataItem, "[\"pkIssueState\"]")%>' >
                                               <%# DataBinder.Eval(Container.DataItem, "[\"sIssueState\"]")%>
                                           </option>
                                           </ItemTemplate>
                                       </asp:Repeater>
                                   </select>  
               </td>
                         </tr>
                       </fieldset>
                  </ItemTemplate>
                  <FooterTemplate >
                       </table>
                  </FooterTemplate>
               </asp:Repeater>

On the <select> i want to show which item is selected.The outer Repeater "IssueSummary_Repeater" gets "pkIssueState" from the datasource.
I should be able to match pkIssueState from Outer repeater with pkIssueState from inner repeater "IssueState_Repeater" and print "selected" within the <option> tag.
I tried this <%# DataBinder.Eval(((System.Web.UI.WebControls.RepeaterItem)Container.Parent.Parent).DataItem, "[\"pkIssueState\"]") %>

but did not work.Can you pls suggest what is missing.

Thursday, December 28, 2006 8:27 PM by techhelp

# re: Accessing Parent DataItem of a Child DataItem

I have a similar problem, but I need to access the outer repeater's e.item.dataitem within the inner repeater. Let me explain this a bit:

I have some students which I show in a datalist. In the item_databound event of the datalist, I get the studentID through e.Item.DataItem("studentID"). The outer datalist contains another datalist inside it that will show the years the student is there in the college. Inside the 2nd datalist, I have a datagrid that will show the attendance of the student. So it is a report of student attendance displayed by student, by year. I need the studentID inside the 2nd datalists ItemDataBound event so that I can bind the datagrid. How can I obtain the studentID from the parent datalist's e.Item.DataItem("studentID") ?? Let me know if you need more info.

Friday, December 07, 2007 4:35 AM by WebTenet

# re: Accessing Parent DataItem of a Child DataItem

To WebTenet:

This seems to be the same issue.  If I'm missing something, then step through the databinding event handler of the outer repeater and look at the properties to find the associated datalist and its associated DataItem.

Friday, December 07, 2007 11:17 AM by vbullinger

# re: Accessing Parent DataItem of a Child DataItem

Vince is on the ball on this one.  I scoured Google and eventually did find the answer in another place but didn't realize it until I saw this page because the answer was missing something important.  The key is in casting the parent object to the correct type, otherwise you'll get an exception, normally:

'DataItem' is not a member of 'System.Web.UI.Control'

In VB, you can do it like this:

Value='<%# DirectCast(Container.Parent.Parent, RepeaterItem).DataItem.MyPropertyName #%>'

Wednesday, December 19, 2007 2:58 PM by John Tolle

# re: Accessing Parent DataItem of a Child DataItem

Thank you!  I've been looking awhile on how to do this.  It's very helpful and simple.

Tuesday, September 09, 2008 8:41 PM by ljardin@hotmail.com

Leave a Comment

(required) 
(required) 
(optional)
(required)