title: Get HTML of a control through a static method
body: <p>I am trying the following:</p>
  
  <pre><code>public partial class PopUps : System.Web.UI.Page {
  
      public string GetContentFromPlaceHolder(string strContentId) {
  
          HtmlTextWriter writer = new HtmlTextWriter(new System.IO.StringWriter());
  
          plcPlaceHolderToGet.RenderControl(writer);
          string strHtml = writer.InnerWriter.ToString();
          return strHtml;
  
      }
  
      public static string GetContent() {
  
          PopUps puToUse = new PopUps();
          string strHtml = puToUse.GetContentFromPlaceHolder();
          return strHtml;
  
      }
  
  }
  </code></pre>
  
  <p>However, when I run the static method <code>GetContent</code> from another page I get this error:</p>
  
  <blockquote>
    <p>object reference not set to an instance of an object  </p>
  </blockquote>
  
  <p>on the line  </p>
  
  <pre><code>plcPlaceHolderToGet.RenderControl(writer);
  </code></pre>
  
  <p>The ID of the control is correct, it just seems like I need to run some kind of instantiation of the controls on the Page before I can get the HTML of one of the controls.  </p>
  
  <p>What am I missing?  </p>
  
  <p>The design view of the page looks like this</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PopUps.aspx.cs" Inherits="PopUps" %&gt;
  
  &lt;asp:PlaceHolder runat="server" id="plcPlaceHolderToGet"&gt;
  
      &lt;div&gt;
          Some more HTML...
      &lt;/div&gt;
  
  &lt;/asp:PlaceHolder&gt;
  
  &lt;asp:PlaceHolder runat="server" id="plcAnotherPlaceHolder"&gt;
  
      &lt;div&gt;
          Some more HTML...
      &lt;/div&gt;
  
  &lt;/asp:PlaceHolder&gt;
  </code></pre>
  
  <p>I am trying to do this to have one place to hold all my HTML. HTML, that sometimes needs to be fetched from other C# code and sometimes through an AJAX call.</p>