title: Asp.net error creating usercontrol from WebMethod
body: <p>I'm referencing the <a href="http://mosesofegypt.net/post/2008/04/GridView-Grouping-Master-Detail-Drill-Down-Using-jQuery-AJAX.aspx" rel="nofollow noreferrer">article reference</a></p>
  
  <p>To create a UserControl dynamically and pass it back to a page via jQuery.</p>
  
  <p>When I just do a simple text in the UserControl "Hello World" everything works great... however, when I tried putting interactive controls on the control, I got the following message in my WebMethod (so the jQuery call stuff is irrelevant) (further testing showed that textboxes, and buttons cause the issue, but not labels)</p>
  
  <p>Error executing child request for handler 'System.Web.UI.Page'</p>
  
  <p>here's the webmethod:</p>
  
  <pre><code>[WebMethod]
  public static string GetCtrl(string cname)
  {
      StringWriter sw = new StringWriter();
  
      Page p = new Page();
      Control c = p.LoadControl("~/Controls/" + cname);
      p.Controls.Add(c);
  
      try
      {
          HttpContext.Current.Server.Execute(p, sw, false);
      }
      catch (Exception err)
      {
          return err.Message;
      }
      sw.Close();
  
      return sw.ToString();
  }
  </code></pre>
  
  <p>The exception is happening during the Server.Execute call.</p>
  
  <p>A solution may not be possible, I'd just like to know why interactive controls cause this.</p>
  
  <p>EDIT:
  Taking advice below I looked deeper and found the inner exception said something to the effect that it must be within a form tag marked with runat="server", while I'm displaying this control on a aspx page within the form tag... my guess is that there's got to be some sort of 'asp.net magic' connection there for the control to operate... regardless, it'd be nice to know how to render a page with controls on it anyway (even if it wasn't usable in this situation)</p>
  
  <p>So I looked at the output of the above code and for those curious:</p>
  
  <p><code>&lt;span id="ctl00_lbl1"&gt;abc: &lt;/span&gt;</code></p>
  
  <p>So the control is rendering properly (with just the label id='lbl1') on the page, however, the control is itself rendered, and the page content is not there at all... Is there a way to generate the page source as well? (I must not understand the way the Server.Execute function works)</p>