C# ITemplate在自定义控件中的使用
我试图弄清楚是否可以在自定义控件中使用C# ITemplate在自定义控件中的使用,c#,asp.net,C#,Asp.net,我试图弄清楚是否可以在自定义控件中使用ITemplate接口。这是我到目前为止所做的 public class Tooltip : Control { public ITemplate ContentTemplate { get; set; } protected override void CreateChildControls() { base.CreateChildControls(); var ctrl = new HtmlGe
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控件的使用
你好
这个想法是它应该返回这样的东西
你好
但结果是这样的
你好
它确实包含模板标签,并且似乎忽略了我所做的任何事情
任何建议都非常感谢我想你应该仔细看看,并按照他们说的去做 你错过了这个:
[
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(TemplateItem))
]
你也应该在课堂上设置这一部分:
[ParseChildren(true)]
我从错误的类继承,当切换到WebControl时,它开始工作
public class Tooltip : WebControl
谢谢,我确实看过了,但它建议我应该创建一个外部控件,我正在寻找一个可以直接实现模板的解决方案。从WebControl而不是Control继承解决了我的问题。