错误在设计模式下Web用户控件使用了Itemplate [英] Error in Web User Control with ITemplate in the Design mode

查看:176
本文介绍了错误在设计模式下Web用户控件使用了Itemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下标记的Web用户控件

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>

在code背后:

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
{
}

和使用如下所示的页面

<%@ 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:

键入System.Web.UI.UserControl'没有命名的公共属性
  内容。

Type 'System.Web.UI.UserControl' does not have a public property named 'Content'.

我怎样才能解决这个问题呢?

How can I solve this issue?

编辑:我修改了code

I modified the code

推荐答案

从MSDN的如何创建模板化ASP.NET用户控件

注意:模板化的ASP.NET用户控件不能在Visual Studio设计支持但是,您可以编译和运行在Visual Studio这个例子。要做到这一点,当你创建ASP.NET页面来测试这个code,更换所有的设计器生成的code与code和标记在示例列表的页面。

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.

这篇关于错误在设计模式下Web用户控件使用了Itemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆