I'm working with a static class in C#, and am trying to use Control.RenderControl()
to get a string / mark-up representation of a Control
.
Unfortunately the control (and all child controls) use event bubbling to populate certain values, for example, when instantiating, then calling RenderControl()
on the following:
public class MyTest : Control
{
protected override void OnLoad(EventArgs e)
{
this.Controls.Add(new LiteralControl("TEST"));
base.OnLoad(e);
}
}
I am returned an empty string, because OnLoad()
is never fired.
Is there a way I can invoke a 'fake' page lifecycle? Perhaps use some dummy Page
control?
I was able to accomplish this by using a local instance of
Page
andHttpServerUtility.Execute
:EDIT
If you need the control to exist inside a
<form />
tag, then simply add anHtmlForm
to the page, and add your control to that form like so: