【问题标题】:Get the current bounded object in a ListView's ItemTemplate获取 ListView 的 ItemTemplate 中的当前有界对象
【发布时间】:2025-12-27 23:50:11
【问题描述】:

我希望能够在ListView 控件的ItemTemplate 中获取当前绑定的对象。

这是我想做的一个例子:

<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>

【问题讨论】:

    标签: c# asp.net data-binding listview


    【解决方案1】:

    您可以通过 DataItem 访问它:

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

    如果你想要一个文本框,例如:

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

    如果你只想要完整的对象:

    <%# (MyType)Container.DataItem %>
    

    【讨论】:

    • 我想要对象的实例,而不是它的属性。
    • @Andreas - 在这种情况下只需 &lt;%# (MyType)Container.DataItem %&gt; 并用它做任何你想做的事情......如果你只需要一个普通的对象,不要施放。
    • 尼克,这正是我想要的。更改您的答案以将其包含在其中,我会接受。
    • @Andreas - 很公平,对下一个出现的人来说更好,更新:)