title: How can I programmatically build a System.Web.UI.Page with a Form and a UserControl?
body: <p>I have this code:</p>
  
  <pre><code>public static string RenderView(string path)
  {
      Page pageHolder = new Page();
      UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
  
      pageHolder.Controls.Add(viewControl);
  
      StringWriter output = new StringWriter();
      HttpContext.Current.Server.Execute(pageHolder, output, false);
  
      return output.ToString();
  }
  </code></pre>
  
  <p>Which is run from:</p>
  
  <pre><code>    [WebMethod]
      public string GetReportsHTML()
      {
          string output = "";
  
          output = ViewManager.RenderView("ReportsControl.ascx");
  
          return output;
      }
  </code></pre>
  
  <p>This is to test rendering ASCX files and spitting them out of a SOAP/REST service.</p>
  
  <p>Problem is, some controls (runat=server ones) fail if they are not encapsulated in a  tag with runat=server.</p>
  
  <p>The solution to that is <a href="https://stackoverflow.com/questions/13777442/control-0-of-type-textbox-must-be-placed-inside-a-form-tag-with-runat-server/13777536#comment20569725_13777536">here</a>, but the solution assumes being inside an ASPX file where I can just edit the markup.</p>
  
  <p>How would I programmatically build a Page, add a Form, set runat=server so that I can follow that solution and add my control to the Form Control?</p>