views:

57

answers:

1

I want to be able to get the current bound object in the ItemTemplate of a ListView control.

Here's an example of what I want to do:

<asp:ListView ID="UserList" runat="server">
    <LayoutTemplate>
        <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
    </LayoutTemplate>
    <ItemTemplate>
        //How can I get the current bound object in here?
    </ItemTemplate>
</asp:ListView>
+1  A: 

You can access it via the DataItem:

<%# DataBinder.Eval(Container.DataItem, "myPropertyName")%>'

If you wanted a textbox for example:

<asp:Label ID="MyProp" runat="server" Text='<%#Eval("myPropertyName") %>' />

If you just want the full object:

<%# (MyType)Container.DataItem %>
Nick Craver
I want the instance of the object, not its property.
Andreas Grech
@Andreas - in that case just `<%# (MyType)Container.DataItem %>` and do whatever you want with it...if you just need an plain object, don't cast.
Nick Craver
Nick, that's exactly what I was looking for. Change your answer to include that in it and I'll accept it.
Andreas Grech
@Andreas - Fair enough, better for the next person that comes along looking as well, updated :)
Nick Craver