2009-08-28 71 views
0

比方說,我有一個自定義模板控件,可以在頁面中使用像這樣:定製ASP.NET模板控制

<AR:CustomControl runat="server"> 
    <ViewTemplate> 
     <span>stuff</span> 
    </ViewTemplate> 
</AR:ModalLink> 

由於只有一個模板字段,該元素不真的有必要。有沒有一種方法可以編輯我的控件,使其可以像?

<AR:CustomControl runat="server"> 
    <span>stuff</span> 
</AR:ModalLink> 

請注意,這是帶有ASCX文件的UserControl。所以我不認爲我可以簡單地繼承像Label這樣的現有控件。

回答

0

發現這裏的解決方案: 「http://alvinzc.blogspot.com/2006/10/aspnet-basic-of-custom-server-control_25.html

一言以蔽之:

[ParseChildren(true, "Content")] 
[PersistChildren(false)] 
public partial class CustomControl : System.Web.UI.UserControl 
{ 
    ... 

    [Browsable(false), 
    PersistenceMode(PersistenceMode.InnerDefaultProperty), 
    TemplateContainer(typeof(Contents)), 
    TemplateInstance(TemplateInstance.Single)] 
    public ITemplate Content { ... } 
} 
+0

鏈接應該http://alvinzc.blogspot.com/2006/10/aspnet-basic-of-定製服務器control_25.html – 2009-10-23 12:44:07