加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET用户控件内部内容

发布时间:2020-12-16 06:52:41 所属栏目:asp.Net 来源:网络整理
导读:我有一个接受title属性的用户控件.我也想在那个用户控件标签内部输入内部 HTML(ASP控件),如下所示: uc:customPanel title="My panel" h1Here we can add whatever HTML or ASP controls we would like./h1 asp:TextBox/asp:TextBox/uc:customPanel 我怎样才
我有一个接受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>

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

谢谢.

解决方法

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

public class Container: Panel,INamingContainer
{
}

然后,您的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; }
}

然后在方法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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读