Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eval() for current item in asp:ListView

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.

like image 948
spaetzel Avatar asked Dec 22 '09 17:12

spaetzel


1 Answers

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>
like image 69
Samuel Neff Avatar answered Sep 17 '22 18:09

Samuel Neff