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

相關問題