【发布时间】: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