ASP.NET用户控件内部内容 [英] ASP.NET User Control inner content

查看:89
本文介绍了ASP.NET用户控件内部内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有接受title属性的用户控件。我也一样,输入内部HTML(ASP也控制)像这样的用户控件标记中:

I have a user control which accepts a title attribute. I would also like that input inner HTML (ASP Controls also) inside of that user control tag like so:

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

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

How can I achieve this? I have the title attribute working correctly.

感谢。

推荐答案

实现一个可扩展面板,实现作INamingContainer的类:

Implement a class that extends Panel and implements INamingContainer:

public class Container: Panel, INamingContainer
{
}

然后,你CustomPanel需要公开类型的容器的属性和类型的ITemplate的另一个属性:

Then, your CustomPanel needs to expose a property of type Container and another property of type ITemplate:

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

然后在方法的CreateChildControls(),补充一点:

if (templateContent != null)
{
    templateContent.InstantiateIn(content);
}

和你会使用这样的:

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

这篇关于ASP.NET用户控件内部内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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