ASP.NET用户控件内部内容

ade*_*ers 4 asp.net user-controls

我有一个接受title属性的用户控件.我也想在那个用户控件标签内部输入内部HTML(ASP控件),如下所示:

<uc:customPanel title="My panel">
     <h1>Here we can add whatever HTML or ASP controls we would like.</h1>
     <asp:TextBox></asp:TextBox>
</uc:customPanel>
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?我有title属性正常工作.

谢谢.

joe*_*age 7

实现一个扩展Panel的类并实现INamingContainer:

public class Container: Panel, INamingContainer
{
}
Run Code Online (Sandbox Code Playgroud)

然后,您的CustomPanel需要公开Container类型的属性和ITemplate类型的另一个属性:

public Container ContainerContent
{
    get
    {
       EnsureChildControls();
       return content;
    }
}
[TemplateContainer(typeof(Container))]
[TemplateInstance(TemplateInstance.Single)]
public virtual ITemplate Content
{
    get { return templateContent; }
    set { templateContent = value; }
}
Run Code Online (Sandbox Code Playgroud)

然后在方法中CreateChildControls(),添加:

if (templateContent != null)
{
    templateContent.InstantiateIn(content);
}
Run Code Online (Sandbox Code Playgroud)

你会像这样使用它:

<uc:customPanel title="My panel"> 
    <Content>    
        <h1>Here we can add whatever HTML or ASP controls we would like.</h1>
        <asp:TextBox></asp:TextBox>
     </Content>
</uc:customPanel>
Run Code Online (Sandbox Code Playgroud)