在我自己的服务器控制中,我想实现类似于 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.aspx Control 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() 这样做之后,但什么都没找到。

有帮助吗?

解决方案

好的,这只是一个问题 用户错误. 。我实例化了 ITemplate 在一个 Panel, ,很明显 Page.FindControl() (这不是递归)行不通。一旦我做到了 Panel.FindControl(), ,一切都奏效了。

其他提示

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top