代码之家  ›  专栏  ›  技术社区  ›  Overflew

ASP.NET网络控制和呈现包含子项

  •  1
  • Overflew  · 技术社区  · 15 年前

    我希望创建一个WebControl,它可以在其中包含标记,并自己动态创建子控件。我面临的问题是,我无法(目前)将包含在标记中的控件(参见下面的示例)与我创建的子控件分开呈现。

    我知道我需要使用以下两个标志设置类:

    [ParseChildren(false)]
    [PersistChildren(true)]
    public class OuterControl : WebControl
    {
      ...
    }
    

    示例标记如下所示:

    <custom:OuterControl>
      <asp:TextBox ...>
    <custom:OuterControl>
    

    在…内 ,我有一些控件需要添加到控件树中,呈现,然后呈现在特定部分中用标记包装的控件。例如。:

    protected override void RenderContents(HtmlTextWriter output)
    {
      EnsureChildControls();
      [ Misc work, render my controls ]
    
      [** Would like to render wrapped children here **]
    
      [ Possibly other misc work ]
    }
    

    如上所述,我可以通过调用 RenderChildren()

    1 回复  |  直到 15 年前
        1
  •  1
  •   Zhaph - Ben Duguid    15 年前

    当我有一个类似的要求(围绕提供的控件构建一组标准控件)时,我最终做了如下工作:

    EnsureChildControls();
    
    Control[] currentControls = new Control[Controls.Count];
    
    if (HasControls()) {
      Controls.CopyTo(currentControls, 0);
      Controls.Clear();
    }
    
    // Misc work, add my controls, etc, e.g.
    Panel contentBox = new Panel { ID = "Content", CssClass = "content_border" };
    // at some point, add wrapped controls to a control collection
    foreach (Control currentControl in currentControls) {
      contentBox.Controls.Add(currentControl);
    }
    
    // Finally, add new controls back into Control collection
    Controls.Add(contentBox);