Building a templated ASP.NET control
<p>I'm playing around with a composite control that uses the ASP.NET templating system.</p>

<p>I want to be able to define a HeaderTemplate and a FooterTemplate in my markup, and programmatically add a usercontrol between the two.</p>

<p>The markup I'm aiming for is something like this:</p>

<pre><code>&lt;asp:DropZone runat="server" ID="LeftZone"&gt;
   &lt;HeaderTemplate&gt;
      &lt;h1&gt;Getting started&lt;/h1&gt;
   &lt;/HeaderTemplate&gt;
   &lt;FooterTemplate&gt;
      &lt;h3&gt;The end of it...&lt;/h3&gt;
   &lt;/FooterTemplate&gt;
&lt;/asp:DropZone&gt;
</code></pre>

<p>My DropZone class looks like this:</p>

<pre><code>public class DropZone : Control, INamingContainer
    {
        private ITemplate headerTemplate;
        private ITemplate footerTemplate;

        [DefaultValue((string)null), Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), TemplateInstance(TemplateInstance.Single)]
        public virtual ITemplate HeaderTemplate
        {
            get { return headerTemplate; }
            set { headerTemplate = value; }
        }

        [DefaultValue((string)null), Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), TemplateInstance(TemplateInstance.Single)]
        public ITemplate FooterTemplate
        {
            get { return footerTemplate; }
            set { footerTemplate = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            EnsureChildControls();
            base.OnInit(e);
        }

        private void AppendTemplate(ITemplate template, Control container)
        {
            if (template == null) return;

            var ph = new PlaceHolder();
            container.Controls.Add(ph);
            template.InstantiateIn(ph);
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();

            AppendTemplate(HeaderTemplate, this);

            Control helloWorld = Page.LoadControl("~/WebParts/HelloWorld.ascx");
            if (helloWorld != null)
            {
                Controls.Add(helloWorld);
            }

            AppendTemplate(FooterTemplate, this);

            ChildControlsCreated = true;
            base.CreateChildControls();
        }
    }
</code></pre>

<p>However, this does not work as the ITemplate fields are never instantiated.</p>

<p>Any help or guidance would be highly appreciated.</p>

<p>Thanks.</p>

<p><strong>UPDATE:</strong> I had to derive my custom control from CompositeControl to get things work as expected.</p>
