title: Retrieve HTML of specific element in ASCX
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/11622541/retrieve-contents-of-htmlgenericcontrol-inside-ascx-webcontrol">Retrieve contents of HtmlGenericControl inside ASCX WebControl</a>  </p>
  </blockquote>
  
  
  
  <p>I have an ASCX control embedded on multiple pages that pulls page-contextual data from a Database, binds it to a repeater, and then has a Button_Click event that will email the contents of the repeater to a user.</p>
  
  <p>I'm having trouble pulling the data server side. I tried using traditional methods (Inside the code behind of the ASCX) such as:</p>
  
  <pre><code>var sw = new System.IO.StringWriter();
  var hw = new HtmlTextWriter(sw);
  quotecontent.RenderControl(hw);
  </code></pre>
  
  <p>But I'm getting an error:</p>
  
  <pre><code>RegisterForEventValidation can only be called during Render();
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
  
  Exception Details: System.InvalidOperationException: RegisterForEventValidation can only be called during Render();
  </code></pre>
  
  <p>It sounds like I may not be able to use an HtmlTextWriter inside of an ASCX file? Is that true or am I doing it wrong? If it is true, how should I access the content of the element?</p>
  
  <p>(This is an issue because the Button_Click happens inside the code of the ASCX user control, not the ASPX Page. Having this in the Page is not an option.)</p>