无法识别ASP .NET嵌套ListView [英] ASP .NET Nested ListView is not recognized

查看:27
本文介绍了无法识别ASP .NET嵌套ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例代码,该代码使用嵌套的ListView:

I have following sample code, which uses nested ListView:

<asp:ListView ID="list" runat="server" ItemPlaceholderID="placeHolder"
        OnItemDataBound="listItemDataBound">
    <ItemTemplate>
        <p><%#Eval("name") %></p>
        <asp:ListView ID="sublist" runat="server" ItemPlaceholderID="subPlaceHolder">
            <ItemTemplate><%#Eval("subName") %></ItemTemplate>
            <LayoutTemplate>
                <asp:PlaceHolder ID="subPlaceHolder" runat="server"></asp:PlaceHolder>
            </LayoutTemplate>
        </asp:ListView>
    </ItemTemplate>
    <LayoutTemplate>
        <asp:PlaceHolder ID="placeHolder" runat="server"></asp:PlaceHolder>
    </LayoutTemplate>
</asp:ListView>

但是在我的脚本代码中嵌套的ListView(子列表)未被识别为变量,因此我无法访问它并提供一些数据绑定.当我在主ListView中添加其他对象(例如DataSource)时,也无法识别该对象.如何访问嵌套的ListView?

But nested ListView (sublist) is not recognized as a variable in my script code, so I can't access it and provide some databinding. When I add some other object inside main ListView (e.g. DataSource), it is also not recognized. How can I access nested ListView?

感谢您的任何建议.

推荐答案

ItemTemplate 中的控件将被创建多次,对于数据源中的每个项目一次,因此编译器无法生成单个代表他们的领域.您需要改用 FindControl :

Controls in the ItemTemplate will be created multiple times, once for each item in your data source, so the compiler cannot generate a single field to represent them. You'll need to use FindControl instead:

protected void listItemDataBound(object sender, ListViewItemEventArgs e)
{
   var sublist = (ListView)e.Item.FindControl("sublist");
   ...
}

这篇关于无法识别ASP .NET嵌套ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆