【发布时间】:2008-11-07 09:43:17
【问题描述】:
我正在设计一个丰富的中继器控件,它需要在运行时添加一些控件(特别是一个无序列表)。
我选择的解决方案是将必要的标记 onInit 分别注入到页眉、项目和页脚模板中。
我可以取出模板(使用 InstantiateIn),然后根据需要添加标记,但我不知道如何将模板重新添加到转发器?
【问题讨论】:
我正在设计一个丰富的中继器控件,它需要在运行时添加一些控件(特别是一个无序列表)。
我选择的解决方案是将必要的标记 onInit 分别注入到页眉、项目和页脚模板中。
我可以取出模板(使用 InstantiateIn),然后根据需要添加标记,但我不知道如何将模板重新添加到转发器?
【问题讨论】:
在过去,我只是简单地处理了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>");
}
}
【讨论】: