This is a useful control that works like a masterpage but is more lightweight.
You can use this control as a wrapper for commonly used html snippets in your code.
<%@ Control Language='C#' AutoEventWireup='true'CodeBehind='WrapperPanel.ascx.cs' Inherits='Controls.WrapperPanel' %> <div class='wrapper'> <div> <asp:Label ID='lbHeader' runat='server' /> </div> <div> <asp:PlaceHolder ID="itemPlaceHolder" runat="server" /> </div> </div> This is the code file for the control.
[PersistChildren(true)]
public partial class WrapperPanel : UserControl
{
public string HeaderText { set { lbHeader.Text = value; } }
private ITemplate _itemTemplate;
[TemplateInstance(TemplateInstance.Single)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate ItemTemplate
{
get { return _itemTemplate; }
set { _itemTemplate = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
itemPlaceHolder.Controls.Clear();
if (ItemTemplate == null) return;
ItemTemplate.InstantiateIn(itemPlaceHolder);
}
}
You would use it as follows:
<AR:wrapperpanel runat="server" HeaderText="Some Control" id-"wrapperpanel"> <ItemTemplate> Your content here </ItemTemplate> </AR:wrapperpanel>
The result would be:
<div class='wrapper'>
<div>
<asp:Label ID='lbHeader' runat='server' />
</div>
<div>
Your content here
</div>
</div>
Now you can focus on what’s important and not worry about the wrapping html code.
Also using the TemplateInstance.Single attribute, you can access controls within your wrapper just as you would without it. If you didn’t use it, you willl have to access them using the FindControl function on wrapper control.