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://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()ましたが、何も見つかりませんでした。

4

2 に答える 2

1

わかりました、これは単にユーザーエラーの問題でした。でインスタンス化しITemplatePanelので、明らかにPage.FindControl()(再帰的ではありません)機能しません。私がやったらPanel.FindControl()、すべてがうまくいきました。

于 2011-01-06T23:27:47.713 に答える
0

過去に、このライブラリを使用して成功しました http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

于 2011-01-06T23:12:55.193 に答える