在我自己的服務器控件實現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