How to load dynamically UserControl that contains asp controls
<p>I have UserControl that has asp control(&lt;asp:Button runat="server" ID="btn" /&gt;) and I want to render it.</p>

<p>I tried 1:</p>

<pre><code>            Page loader = new Page();

            Control ctrl = loader.LoadControl("WebUserControl1.ascx");
            loader.Controls.Add(ctrl);
            loader.ProcessRequest(context);

            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            loader.RenderControl(htw);
</code></pre>

<p>It doesn't works because it says that each control that has runat="server" must be in Form.</p>

<p>I also tried 2:</p>

<p>Because loader.Form is null, I create HtmlForm and added the ctrl to it and then added HtmlForm to loader's Control, BUT it says that it has 2 HtmlForm. So, instead I "injected" HtmlForm to loader like this</p>

<pre><code>            Page loader = new Page();

            HtmlForm form = new HtmlForm();
            var method = loader.GetType().GetMethod("SetForm", BindingFlags.NonPublic | BindingFlags.Instance);
            method.Invoke(loader, new object[] { form });

            Control ctrl = loader.LoadControl("WebUserControl1.ascx");
            loader.Form.Controls.Add(ctrl);
            loader.ProcessRequest(context);

            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            loader.RenderControl(htw);
</code></pre>

<p>Now no error is occurred, BUT StringWriter is empty</p>

<p>Where is my mistake.
Please don't tell me to add Page to solution and override OnRender. I want to do it as I mentioned above.</p>

<p>Thank you</p>
