Error in Web User Control with ITemplate in the Design mode

安稳与你 提交于 2020-01-15 11:25:48

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6834315/error-in-web-user-control-with-itemplate-in-the-design-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!