可能有一个自定义服务器控件的内部控制? [英] Possible to have a inner control on a custom server control?
问题描述
我希望能够做这样的事情:
< UI:标签标题=A动听的名字>
< TabTemplate>
< ASP:文字=服务器ID =SetMe>在文字或东西< / ASP:文字>
< / TabTemplate>
< / UI:标签>
也能够做到:
< UI:标签标题=A动听的名字>
< TabTemplate>
< ASP:DataList控件=服务器ID =BindMe>< / ASP:DataList控件>
< / TabTemplate>
< / UI:标签>
答code我终于想出了:
[ParseChildren(真)]
公共类节点:SiteMapNodeBaseControl,作INamingContainer
{
私人了Itemplate tabTemplate;
[可浏览(假),
默认值(NULL)
说明(以下简称标签模板。)
PersistenceMode(PersistenceMode.InnerProperty)
TemplateContainer(typeof运算(TabTemplate))]
公开了Itemplate TabTemplate
{
{返回tabTemplate; }
集合{tabTemplate =价值; }
}
保护覆盖无效的CreateChildControls()
{
如果(TabTemplate!= NULL)
{
Controls.Clear();
TabTemplate I =新TabTemplate();
TabTemplate.InstantiateIn(ⅰ);
Controls.Add被(ⅰ);
}
}
保护覆盖无效渲染(HtmlTextWriter的作家)
{
EnsureChildControls();
base.Render(作家);
}
}
公共类TabTemplate:控制,作INamingContainer
{
}
该ParseChildren属性告诉.NET是否将控件的儿童属性或作为对照。为了您的第一个例子,你要正确对待儿童作为对照,所以加
[ParseChildren(ChildrenAsProperties = FALSE)]
对于第二个,你要ChildrenAsProperties = TRUE,和类型的ITemplate的TabTemplate财产。有一些管道后,参与,其中这个MSDN样本描述。它不会增加很多价值,如果你只需要一个模板,虽然。
I would like to be able to do something like:
<ui:Tab Title="A nice title">
<TabTemplate>
<asp:Literal runat="server" ID="SetMe">With Text or Something</asp:Literal>
</TabTemplate>
</ui:Tab>
but also be able to do:
<ui:Tab Title="A nice title">
<TabTemplate>
<asp:DataList runat="server" ID="BindMe"></asp:DataList>
</TabTemplate>
</ui:Tab>
Answer code I eventually came up with:
[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
{
}
The ParseChildren attribute tells .NET whether to treat your control's children as properties or as controls. For your first example, you want to treat children as controls, so add
[ ParseChildren(ChildrenAsProperties = false) ]
For the second, you want ChildrenAsProperties=true, and a TabTemplate property of type ITemplate. There's some plumbing involved after that, which this MSDN sample describes. It doesn't add a lot of value if you only need one template, though.
这篇关于可能有一个自定义服务器控件的内部控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!