ASP.NET自定义控件,可以模板字段有属性? [英] ASP.NET custom control, can template fields have attributes?
本文介绍了ASP.NET自定义控件,可以模板字段有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
< UC:AdmiralAckbar =服务器ID =myCustomControl>
<警告SomeAttribute =这是个陷阱>
我的数据
< /警告>
< / UC:AdmiralAckbar>
我不知道如何添加SomeAttribute。任何想法?
无属性code是:
私人了Itemplate警告= NULL; [TemplateContainer(typeof运算(作INamingContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)
公开了Itemplate警告
{
得到
{
返回警告;
}
组
{
警告=价值;
}
}
解决方案
答案是肯定的。
有关这个你应该建立一个类型,它实现了Itemplate
接口,并添加自定义属性/属性那里(我添加的属性名称
在我的例子);还添加了类从继承收集和LT; YourTemplate方式>
下面是这样做的例子:
公共类TemplateList:收集< TemplateItem> {}公共类TemplateItem:了Itemplate
{
公共字符串名称{;组; } 公共无效InstantiateIn(控制容器)
{
VAR DIV =新HtmlGenericControl(分区);
div.InnerText = this.Name; container.Controls.Add(DIV);
}
}
和控制本身:
[ParseChildren(真正的模板),PersistChildren(假)]
公共类TemplateLibrary:控制
{
公共TemplateLibrary()
{
模板=新TemplateList();
} [PersistenceMode(PersistenceMode.InnerProperty)
公共TemplateList模板{搞定;组; } 保护覆盖无效RenderChildren(HtmlTextWriter的作家)
{
的foreach(在模板VAR项)
{
item.InstantiateIn(本);
} base.RenderChildren(作家);
}
}
和最后使用的示例:
<我:TemplateLibrary =服务器>
<我:TemplateItem NAME =你好/>
<我:TemplateItem NAME =有/>
< /我:TemplateLibrary>
顺便说一句,你也可以使用它作为:
<我:TemplateLibrary =服务器>
<模板和GT;
<我:TemplateItem NAME =你好/>
<我:TemplateItem NAME =有/>
< /模板>
< /我:TemplateLibrary>
的效果将是相同的。
For example:
<uc:AdmiralAckbar runat="server" id="myCustomControl">
<Warning SomeAttribute="It's A Trap">
My Data
</Warning>
</uc:AdmiralAckbar>
I'm not sure how to add SomeAttribute. Any ideas?
Code without the attribute is:
private ITemplate warning = null;
[TemplateContainer(typeof(INamingContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate Warning
{
get
{
return warning;
}
set
{
warning = value;
}
}
解决方案
The answer is yes.
For this you should create a type which implements ITemplate
interface and add a custom property/properties there (I added property Name
in my example); also add a class which inherits from Collection<YourTemplate>
.
Here is an example of doing that:
public class TemplateList : Collection<TemplateItem> { }
public class TemplateItem : ITemplate
{
public string Name { get; set; }
public void InstantiateIn(Control container)
{
var div = new HtmlGenericControl("div");
div.InnerText = this.Name;
container.Controls.Add(div);
}
}
and a control itself:
[ParseChildren(true, "Templates"), PersistChildren(false)]
public class TemplateLibrary : Control
{
public TemplateLibrary()
{
Templates = new TemplateList();
}
[PersistenceMode(PersistenceMode.InnerProperty)]
public TemplateList Templates { get; set; }
protected override void RenderChildren(HtmlTextWriter writer)
{
foreach (var item in Templates)
{
item.InstantiateIn(this);
}
base.RenderChildren(writer);
}
}
and finally an example of usage:
<my:TemplateLibrary runat="server">
<my:TemplateItem Name="hello" />
<my:TemplateItem Name="there" />
</my:TemplateLibrary>
BTW, you could also use it as:
<my:TemplateLibrary runat="server">
<Templates>
<my:TemplateItem Name="hello" />
<my:TemplateItem Name="there" />
</Templates>
</my:TemplateLibrary>
the effect will be the same.
这篇关于ASP.NET自定义控件,可以模板字段有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文