0

在我自己的服务器控件中,我想实现类似于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)。我尝试转换为 my 的类型ITemplate,但出现错误:

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

我错过了什么?

编辑: 我刚刚发现:http 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. ://www.nikhilk.net/SingleInstanceTemplates.aspx 。它似乎只用于用户控件?我这样做后尝试过Page.FindControl(),但没有找到任何东西。

4

2 回答 2

1

好的,这只是用户错误的问题。我实例化了ITemplatein a Panel,所以很明显Page.FindControl()(这不是递归的)不起作用。一旦我这样做了Panel.FindControl(),一切都奏效了。

于 2011-01-06T23:27:47.713 回答
0

在过去,我成功地使用了这个库 http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

于 2011-01-06T23:12:55.193 回答