【问题标题】:How to add controls to ItemTemplate (Repeater) at run time?如何在运行时向 ItemTemplate (Repeater) 添加控件?
【发布时间】:2008-11-07 09:43:17
【问题描述】:

我正在设计一个丰富的中继器控件,它需要在运行时添加一些控件(特别是一个无序列表)。

我选择的解决方案是将必要的标记 onInit 分别注入到页眉、项目和页脚模板中。
我可以取出模板(使用 InstantiateIn),然后根据需要添加标记,但我不知道如何将模板重新添加到转发器?

【问题讨论】:

    标签: c# repeater


    【解决方案1】:

    在过去,我只是简单地处理了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>");
        }
    }
    

    【讨论】:

    • 我可以从复合控件中执行此操作吗?
    • 我不确定你的困惑在哪里,亚当。也许您可以编辑您的问题并添加一些代码或进一步解释。
    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 2011-11-07
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多