2011-01-06 119 views
0

在我自己的服務器控件實現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()但它沒有找到任何東西。

回答

1

好吧,這只是用戶錯誤的問題。我在Panel中實例化了ITemplate,所以顯然Page.FindControl()(這不是遞歸的)將不起作用。一旦我做了Panel.FindControl(),一切都奏效了。