Reputation: 8192
I'm looking to create a WebControl that can house markup inside it, and do its own dynamic creation of child controls. The issue I'm having is that I can't (yet) render the controls housed in the markup (see example below) separate from the child controls I create.
I'm aware I need to set up the class with these 2 flags:
[ParseChildren(false)]
[PersistChildren(true)]
public class OuterControl : WebControl
{
...
}
And sample markup would look like:
<custom:OuterControl>
<asp:TextBox ...>
<custom:OuterControl>
Inside RenderContents(), I have some controls I need to add to the control tree, render, then render the ones wrapped in markup in a particular part. E.g.:
protected override void RenderContents(HtmlTextWriter output)
{
EnsureChildControls();
[ Misc work, render my controls ]
[** Would like to render wrapped children here **]
[ Possibly other misc work ]
}
As stated, I can either get my code-created controls to render twice from calling RenderChildren(), or the wrapped controls to not render at all by removing that line. Darn.
Thoughts?
Upvotes: 1
Views: 1743
Reputation: 26976
When I had a similar requirement (building a standard set of controls around the controls supplied), I ended up doing something like this:
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);
Which has held up quite well.
Upvotes: 1