1

Possible Duplicate:
Retrieve contents of HtmlGenericControl inside ASCX WebControl

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.

I'm having trouble pulling the data server side. I tried using traditional methods (Inside the code behind of the ASCX) such as:

var sw = new System.IO.StringWriter();
var hw = new HtmlTextWriter(sw);
quotecontent.RenderControl(hw);

But I'm getting an error:

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();

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?

(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.)

Community
  • 1
  • 1
Wesley
  • 5,381
  • 9
  • 42
  • 65
  • Is this related to ASP.NET development? And/or C#? The `html` and `render` labels don't look helpful/right. Consider changing if these other labels are accurate. – Jared Farrish Jul 23 '12 at 23:42
  • If my understanding is correct, when the button is pressed you need to get back the data previously bound to the repeater to send it via email? – Jupaol Jul 24 '12 at 00:03
  • @Jupaol That is correct. The HTML is rendered inside of a div with the attribute runat=server – Wesley Jul 24 '12 at 00:11
  • Then I don't understand the logic behind: `var sw = new System.IO.StringWriter(); var hw = new HtmlTextWriter(sw); quotecontent.RenderControl(hw);` Why are you trying to render something? It makes me think that you are **facing problems when rendering data inside the repeater** – Jupaol Jul 24 '12 at 00:22
  • Because you can't call InnerHtml() in C# on a non-literal control. – Wesley Jul 24 '12 at 00:25
  • If you have a way to get the contents of an HtmlGenericControl as a string in code behind in the context of a Button_Click INSIDE of an ASCX code behind, I'd love to hear it :) – Wesley Jul 24 '12 at 00:26

1 Answers1

1

If my understanding is correct this is the output you are looking for:

enter image description here

I posted the code to get this result in your simplified question

Community
  • 1
  • 1
Jupaol
  • 21,107
  • 8
  • 68
  • 100