2
我正在设计一个丰富的中继器控件,它需要一些控件(特别是只是一个无序列表)在运行时添加到它。
我选择的解决方案是分别在页眉,项目和页脚模板中注入nesseccary标记onInit。
我可以将模板取出(使用InstantiateIn),然后根据需要添加标记,但是我不知道如何将模板添加回中继器?如何在运行时将控件添加到ItemTemplate(Repeater)?
我正在设计一个丰富的中继器控件,它需要一些控件(特别是只是一个无序列表)在运行时添加到它。
我选择的解决方案是分别在页眉,项目和页脚模板中注入nesseccary标记onInit。
我可以将模板取出(使用InstantiateIn),然后根据需要添加标记,但是我不知道如何将模板添加回中继器?如何在运行时将控件添加到ItemTemplate(Repeater)?
在过去,我只是处理了ItemDataBound Event
,并修改了当前的RepeaterItem
,无论我需要做什么。
实施例:
private void Repeater1_ItemDataBound(object Sender, RepeaterItemEventArgs e)
{
// Make sure you filter for the item you are after
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
PlaceHolder listLocation = (PlaceHolder)e.Item.FindControl("listPlaceHolder");
var subItems = ((MyClass)e.Item.DataItem).SubItems;
listLocation.Controls.Add(new LiteralControl("<ul>");
foreach(var item in subItems)
{
listLocation.Controls.Add(new LiteralControl("<li>" + item + "</li>"));
}
listLocation.Controls.Add(new LiteralControl("</ul>");
}
}
将我能够从复合控制内这样做呢? – 2008-11-07 11:55:26