2009-12-16 131 views
3

我有一个用户控件接受一个标题属性。我也想,像这样用户控件标记的内部的输入内HTML(ASP也控制):ASP.NET用户控件内容

<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属性正常工作。

谢谢。

+0

我认为你不能做到这一点......但是哟可以做到这一点在乌尔用户控制 – 2009-12-16 04:26:39

+0

的的.ascx你可以指定一个位吗?在我的ascx文件中,我有<%=title%>和<%=content%>,它可以正常工作,但将自定义HTML传递给content属性是一件很痛苦的事情。 – ademers 2009-12-16 04:29:27

+0

System.Web.UI.UserControl没有属性文本框 – 2009-12-16 04:30:58

回答

7

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

public class Container: Panel, INamingContainer 
{ 
} 

然后,你CustomPanel需要公开类型的容器的属性和类型的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> 
0

您需要确保EnsureChildControls被调用。通过CreateChildControls基本方法可以实现多种方式,但您可以简单地执行此操作以获取要呈现的自定义控件的内部内容。当你需要记住状态和触发事件时,它变得更加复杂,但对于纯HTML,这应该起作用。

protected override void Render(HtmlTextWriter writer) { 
    EnsureChildControls(); 
    base.Render(writer); 
}