簡體   English   中英

ITemplate在自定義控件中的用法

[英]Usage of ITemplate in custom control

我試圖弄清楚是否可以在ITemplate中使用ITemplate接口。 到目前為止,這是我所做的。

public class Tooltip : Control
{
    public ITemplate ContentTemplate { get; set; }

    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        var ctrl = new HtmlGenericControl("a");

        if (ContentTemplate != null)
        {
            ContentTemplate.InstantiateIn(ctrl);
        }

        this.Controls.Add(ctrl);
    }
}

asp.net控件的用法

<gc:Tooltip runat="server">
    <ContentTemplate>
        hello
    </ContentTemplate>
</gc:Tooltip>

這個想法是,它應該返回是這樣的

<a>hello</a>

但是結果看起來像這樣

<ContentTemplate>
  hello
</ContentTemplate>
<a></a>

它確實包含模板標簽,並且似乎忽略了我所做的任何事情。

任何建議表示贊賞

我認為您應該在這里看看並按照他們已經說過的做。

您錯過了:

  [
      PersistenceMode(PersistenceMode.InnerProperty),
      TemplateContainer(typeof(TemplateItem))
  ]

另外,您還應該在課堂上設置此部分:

[ParseChildren(true)]

我從錯誤的類繼承而來,當切換到WebControl時,它開始起作用

public class Tooltip : WebControl

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM