views:

229

answers:

1

In an ASP:ListView I want to pass an entire object to a child control within the ItemTemplate, not just a single property of the current object.

Here is the code I want to work:

<asp:ListView ID="answers" runat="server">
    <LayoutTemplate>
       <div id="itemPlaceholder" runat="server" />
    </LayoutTemplate>
    <ItemTemplate>
       <div>
          <uc2:DocumentHeader runat="server" Document="Eval(%# Eval("this") %> />
               <p><%# Eval("Body") %></p>
        </div>
     </ItemTemplate>
</asp:ListView>

The Document property of the DocumentHeader expects the entire Document object, whereas "Body" is a property of Document.

Obviously, I could just create a new property within Document or use a LINQ query to generate a new class with the property I want, I just want to know if there is an easier, more direct way to get what I want.

+1  A: 

You can bind the context object using <%# Container.DataItem %>. You probably will need to cast it to whatever "Document" expects.

<asp:ListView ID="answers" runat="server">
    <LayoutTemplate>
       <div id="itemPlaceholder" runat="server" />
    </LayoutTemplate>
    <ItemTemplate>
       <div>
          <uc2:DocumentHeader runat="server" Document="<%# Container.DataItem %>" />
          <p><%# Eval("Body") %></p>
        </div>
     </ItemTemplate>
</asp:ListView>
Sam
That does it. I didn't need to cast the item, that was done automatically.
spaetzel