Question

In my own server control, I would like to implement something similar to the ListView:

<asp:ListView runat="server">
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" id="itemPlaceholder" />
    </LayoutTemplate>
</asp:ListView>

I have created an ITemplate property, I can set the layout in the aspx page, and I am doing ITemplate.InstantiateIn(myControl).

But I can't figure out how to insert controls at the placeholder. I'm guessing it would be something like MyTemplate.FindControl("itemPlaceholder").Controls.Add(myControl). I tried casting to the type of my ITemplate, but I get the error:

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

What am I missing?

Edit: I just found this: 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.. It seems to be only for user controls? I tried Page.FindControl() after doing this but it didn't find anything.

Was it helpful?

Solution

Ok, this was simply a matter of user error. I instantiated the ITemplate in a Panel, so obviously Page.FindControl() (which is not recursive) wouldn't work. Once I did Panel.FindControl(), everything worked.

OTHER TIPS

In the past I have used this library with sucess http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top