1

我希望能够做一些事情,如:可能在自定义服务器控件上有一个内部控件?

<ui:Tab Title="A nice title"> 
    <TabTemplate> 
    <asp:Literal runat="server" ID="SetMe">With Text or Something</asp:Literal> 
    </TabTemplate> 
</ui:Tab> 

而且还能够做到:

<ui:Tab Title="A nice title"> 
    <TabTemplate> 
    <asp:DataList runat="server" ID="BindMe"></asp:DataList> 
    </TabTemplate> 
</ui:Tab> 

答案的代码,我终于想出了:

[ParseChildren(true)] 
public class Node : SiteMapNodeBaseControl, INamingContainer 
{ 
    private ITemplate tabTemplate; 
    [Browsable(false), 
    DefaultValue(null), 
    Description("The tab template."), 
    PersistenceMode(PersistenceMode.InnerProperty), 
    TemplateContainer(typeof(TabTemplate))] 
    public ITemplate TabTemplate 
    { 
     get { return tabTemplate; } 
     set { tabTemplate = value; } 
    } 
    protected override void CreateChildControls() 
    { 
     if (TabTemplate != null) 
     { 
      Controls.Clear(); 
      TabTemplate i = new TabTemplate(); 
      TabTemplate.InstantiateIn(i); 
      Controls.Add(i); 
     } 
    } 
    protected override void Render(HtmlTextWriter writer) 
    { 
     EnsureChildControls(); 
     base.Render(writer); 
    } 
} 


public class TabTemplate : Control, INamingContainer 
{ 
} 

回答

1

的ParseChildren属性告诉.NET是否将您的控件的子项视为属性或控件。为了您的第一个例子,你要正确对待儿童作为对照,所以加

[ ParseChildren(ChildrenAsProperties = false) ] 

对于第二个,你想ChildrenAsProperties = TRUE,和类型的ITemplate的TabTemplate财产。之后有一些管道工程,其中this MSDN sample描述。但是,如果您只需要一个模板,则不会增加很多价值。

+0

我粘贴了迄今为止我所拥有的。几乎...:P – rball 2009-03-05 23:04:20

相关问题