Eval () for the current item in asp: ListView

In ASP: ListView, I want to pass the entire object to a child in an ItemTemplate, not just one 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 The DocumentHeader expects the entire Document object, while the Body is the Document property.

Obviously, I could just create a new property in the document or use the LINQ query to generate a new class with the desired property. I just want to know if there is a simpler and more direct way to get what I want.

+6
c # listview
source share
1 answer

You can bind a context object using <% # Container.DataItem%>. You will probably need to give it to everything that the 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> 
+9
source share

All Articles