【问题标题】:How to implement LayoutTemplate with a PlaceHolder如何使用 PlaceHolder 实现 LayoutTemplate
【发布时间】:2011-06-04 23:44:50
【问题描述】:

在我自己的服务器控件中,我想实现类似于ListView

<asp:ListView runat="server">
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" id="itemPlaceholder" />
    </LayoutTemplate>
</asp:ListView>

我已经创建了ITemplate属性,可以在aspx页面中设置布局,我正在做ITemplate.InstantiateIn(myControl)

但我不知道如何在占位符处插入控件。我猜它会类似于MyTemplate.FindControl("itemPlaceholder").Controls.Add(myControl)。我尝试转换为 ITemplate 的类型,但出现错误:

Unable to cast object of type 'System.Web.UI.CompiledTemplateBuilder' to type 'MyNamespace.MyLayoutTemplate'

我错过了什么?

编辑:我刚刚发现了这个:http://www.nikhilk.net/SingleInstanceTemplates.aspxControl developers can define templates to be single instance using metadata which causes the ID'd controls within the template contents to be promoted to the page level... The parser and code-generator together work behind the scenes to add member fields, and initialize them at the right time.。它似乎只用于用户控件?这样做后我尝试了Page.FindControl(),但没有找到任何东西。

【问题讨论】:

    标签: asp.net controls placeholder itemplate


    【解决方案1】:

    【讨论】:

    • 这看起来像是用于在回发的占位符中保留控件。我什至无法添加控件...我需要以某种方式在我的ITemplate 中获取对PlaceHolder 的引用,以便我可以执行Controls.Add()
    【解决方案2】:

    好的,这只是用户错误的问题。我在Panel 中实例化了ITemplate,所以很明显Page.FindControl()(它不是递归的)不起作用。一旦我做了Panel.FindControl(),一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-22
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 2012-06-14
      • 2014-05-01
      相关资源
      最近更新 更多