2008-11-07 54 views
2

我正在設計一個豐富的中繼器控件,它需要一些控件(特別是只是一個無序列表)在運行時添加到它。

我選擇的解決方案是分別在頁眉,項目和頁腳模板中注入nesseccary標記onInit。
我可以將模板取出(使用InstantiateIn),然後根據需要添加標記,但是我不知道如何將模板添加回中繼器?如何在運行時將控件添加到ItemTemplate(Repeater)?

回答

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>"); 
    } 
} 
+0

將我能夠從複合控制內這樣做呢? – 2008-11-07 11:55:26

相關問題