1

I'm building a templated web control for an asp .net web forms application. Most of it runs fine, but one problem I’m stuck with is that all the controls inside my template are null at runtime.

For instance if my control is marked up like this:

<fscc:CollapsiblePanel runat="server" ID="cpExample1" ImageControlId="image1" CssClass="curved">
    <Header>
        this is my header
        <asp:Image ImageUrl="imageurl" runat="server" ID="image1" />
    </Header>
    <Body>
        this is the body
    </Body>
</fscc:CollapsiblePanel>

And my page load is like this:

protected void Page_Load(object sender, EventArgs e)
{
    image1.ImageUrl = Paths.Images.expand_jpg;
}

It will compile and run, but image1 is always null.

I have added these attributes to the class:

[ParseChildren(true), PersistChildren(false)]
public class MyControl : WebControl

And to the template property I have added these:

[Browsable(false),
 PersistenceMode(PersistenceMode.InnerProperty),
 TemplateInstance(TemplateInstance.Single)]
public virtual ITemplate Header { get; set; }

So far as I can tell this is enough for the image1 control to be available. Also the tooling seems to recognise control and puts the image1 into the .designer.cs file, but it is always null. What am I missing?

ilivewithian
  • 19,476
  • 19
  • 103
  • 165

0 Answers0