在我自己的服务器控件实现LayoutTemplate模板,我想实现类似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()
但它没有找到任何东西。
这看起来像是为了在回传中占位符中持久控制。我甚至无法添加控件......我需要以某种方式在我的“ITemplate”中获取对“PlaceHolder”的引用,以便我可以执行Controls.Add() – 2011-01-06 23:18:48