I have a web user control with the following markup
<table>
<tr>
<td>
<h1>
<%= this.Title %></h1>
</td>
</tr>
<tr>
<td>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</td>
</tr>
<tr>
<td>
<h2>
Footer</h2>
</td>
</tr>
</table>
the code behind:
[ParseChildren(true, "Content"), PersistChildren(true)]
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public string Title { get; set; }
[PersistenceMode(PersistenceMode.InnerDefaultProperty),
TemplateContainer(typeof(ContentContainer)),
TemplateInstance(TemplateInstance.Single),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ITemplate Content { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
PlaceHolder1.Controls.Clear();
var container = new ContentContainer();
this.Content.InstantiateIn(container);
PlaceHolder1.Controls.Add(container);
}
}
public class ContentContainer : Control, INamingContainer
{
}
and using in a page like the following
<%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" Title="The Title">
<Content>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></Content>
</uc1:WebUserControl1>
When I run the page it executed well. when I view the page in Design mode I got the following error:
Type 'System.Web.UI.UserControl' does not have a public property named 'Content'.
How can I solve this issue?
EDIT: I modified the code
You need to add the parse children property to your user control class as below.
This means that in the ContentProvider control, its inner controls will be parsed and added into its Children property. In design time, they will be persisted as its child controls. Check this link for details.
From MSDN How to: Create Templated ASP.NET User Controls