1

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

Ahmed Magdy
  • 5,956
  • 8
  • 43
  • 75

2 Answers2

3

From MSDN How to: Create Templated ASP.NET User Controls

Note: Templated ASP.NET user controls are not supported in the Visual Studio designer. However, you can compile and run this example in Visual Studio. To do so, when you create ASP.NET pages to test this code, replace all the designer-generated code in the pages with the code and markup in the example listings.

wonkim00
  • 637
  • 3
  • 9
0

You need to add the parse children property to your user control class as below.

ParseChildren(true, "Content")
public partial class WebUserControl1 : System.Web.UI.UserControl

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.

coder net
  • 3,447
  • 5
  • 31
  • 40
  • how about [ParseChildren(true, "Content"), PersistChildren(true)]. You may have to close the page and reopen it to resolve any caching issues. Did you look through the article that i linked to. It addresses this kind of designer issues. – coder net Jul 26 '11 at 18:22
  • also, try setting the persistence mode of Content to PersistenceMode(PersistenceMode.InnerDefaultProperty) – coder net Jul 26 '11 at 18:25
  • I updated the question with what I did. Don't worry about reopening the page it's something very logic and I even after modifying the code I rebuild the project and reopen the page. – Ahmed Magdy Jul 26 '11 at 18:32
  • The persistence mode section of your Content ITemplate is wrong in the code you updated. I hope you tried setting it to InnerDefaultProperty. – coder net Jul 26 '11 at 18:34
  • @codernet let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1856/discussion-between-pr0fess0rx-and-coder-net) – Ahmed Magdy Jul 26 '11 at 18:43
  • My final comment. If this does not work, i'm out. It looks like instead of persist children on the user control class, it should be [ParseChildren( ChildrenAsProperties = true ) ] – coder net Jul 26 '11 at 18:43