I have created a custom server control with properties implementing the ITemplate interface. It is basically a custom panel box with a header, body & footer. Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ACME.Web.Controls
{
[ParseChildren(true)]
[DefaultProperty("Text")]
[ToolboxData("")]
public class Panel : WebControl, INamingContainer
{
private ITemplate _header = null;
private Control _headerControl = null;
private Control _headerContainerControl = null;
private ITemplate _content = null;
private Control _contentControl = null;
private Control _contentContainerControl = null;
private ITemplate _footer = null;
private Control _footerControl = null;
private Control _footerContainerControl = null;
[PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(TemplateControl)),
TemplateInstance(TemplateInstance.Single)]
public ITemplate Header
{
get { return _header; }
set { _header = value; }
}
[PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(TemplateControl)),
TemplateInstance(TemplateInstance.Single)]
public ITemplate Content
{
get { return _content; }
set { _content = value; }
}
[PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(TemplateControl)),
TemplateInstance(TemplateInstance.Single)]
public ITemplate Footer
{
get { return _footer; }
set { _footer = value; }
}
protected override void CreateChildControls()
{
Controls.Clear();
if (this.ID == null) this.ID = "panel-" + Guid.NewGuid().ToString();
this.CssClass = "panel";
if (this.Header == null)
{
this.CssClass += " without-header";
}
else
{
this.CssClass += " with-header";
_headerControl = new System.Web.UI.WebControls.Panel { CssClass = "panel-header" };
_headerControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-header-l" });
_headerControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-header-c" });
_headerControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-header-r" });
_headerControl.Controls[1].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-header-wrapper" });
_headerControl.Controls[1].Controls[0].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "wrapper" });
_headerContainerControl = new Control();
if (this.Header != null)
{
Header.InstantiateIn(_headerContainerControl);
}
else
{
_headerContainerControl.Controls.Add(new LiteralControl("Test"));
}
_headerControl.Controls[1].Controls[0].Controls[0].Controls.Add(_headerContainerControl);
Controls.Add(_headerControl);
}
_contentControl = new System.Web.UI.WebControls.Panel { CssClass = "panel-body" };
_contentControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-t" });
_contentControl.Controls[0].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-t-l" });
_contentControl.Controls[0].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-t-c" });
_contentControl.Controls[0].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-t-r" });
_contentControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-m" });
_contentControl.Controls[1].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-m-l" });
_contentControl.Controls[1].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-m-c" });
_contentControl.Controls[1].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-m-r" });
_contentControl.Controls[1].Controls[1].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-wrapper" });
_contentControl.Controls[1].Controls[1].Controls[0].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "scroll-wrapper" });
_contentControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-b" });
_contentControl.Controls[2].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-b-l" });
_contentControl.Controls[2].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-b-c" });
_contentControl.Controls[2].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-b-r" });
_contentContainerControl = new Control();
if (this.Content != null)
{
Content.InstantiateIn(_contentContainerControl);
}
else
{
_contentContainerControl.Controls.Add(new LiteralControl("Test"));
}
_contentControl.Controls[1].Controls[1].Controls[0].Controls[0].Controls.Add(_contentContainerControl);
Controls.Add(_contentControl);
if (this.Footer == null)
{
this.CssClass += " without-footer";
}
else
{
this.CssClass += " with-footer";
_footerControl = new System.Web.UI.WebControls.Panel { CssClass = "panel-footer" };
_footerContainerControl = new Control();
if (this.Footer != null)
{
Footer.InstantiateIn(_footerContainerControl);
}
else
{
_footerContainerControl.Controls.Add(new LiteralControl("Test"));
}
_footerControl.Controls.Add(_footerContainerControl);
Controls.Add(_footerControl);
}
}
protected override HtmlTextWriterTag TagKey
{
get { return HtmlTextWriterTag.Div; }
}
}
}
It works perfectly but one cannot really access controls within the ITemplates from the code behind. I would like to be able to add and remove controls from the ITemplate regions of my custom control. What would be the best way to do this?