我正在设计一个丰富的中继器控件,它需要在运行时添加一些控件(特别是一个无序列表)。
我选择的解决方案是将必要的标记 onInit 分别注入到页眉、项目和页脚模板中。
我可以取出模板(使用 InstantiateIn),然后根据需要添加标记,但我不知道如何将模板添加回转发器?
Adam Naylor
问问题
11989 次
1 回答
4
在过去,我只是简单地处理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-07T11:47:25.287 回答