在我自己的服务器控件中,我希望实现类似于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(),但没有找到任何东西。
发布于 2011-01-06 23:27:47
好的,这只是用户错误的问题。我在一个ITemplate中实例化了Panel,所以很明显,Page.FindControl() (不是递归的)不能工作。一旦我做了Panel.FindControl(),一切都成功了。
发布于 2011-01-06 23:12:55
在过去,我使用过这个库和sucess http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx。
https://stackoverflow.com/questions/4620918
复制相似问题