get HTML of control in ASP.NET 2.0

G

Guest

Hi All.

All I want to do is get the HTML of a control on my page at postback into a
string in my code behind.
In ASP.NET 1.0 this used to be simple:

Dim lStringWriter As System.IO.StringWriter = New System.IO.StringWriter
Dim lHtmlTextWriter As System.Web.UI.HtmlTextWriter = New
System.Web.UI.HtmlTextWriter(lStringWriter)
SomeServerControl.RenderControl(lHtmlTextWriter)
Dim lStrControlHTML As String = lStringWriter.ToString()

This however would generate an error in ASP.NET 1.0 because the control
would be treated as if not inside a form tag.
To go around this problem all I had to do is overwrite this check like so:

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As
System.Web.UI.Control)
End Sub

But now in ASP.NET 2.0 this does not work anymore. I get this error message:

RegisterForEventValidation can only be called during Render();

on this line:
SomeServerControl.RenderControl(lHtmlTextWriter)

this is the trace stack:
[InvalidOperationException: RegisterForEventValidation can only be called
during Render();]
System.Web.UI.ClientScriptManager.RegisterForEventValidation(String
uniqueId, String argument) +239
System.Web.UI.WebControls.TextBox.AddAttributesToRender(HtmlTextWriter
writer) +2370
System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter
writer) +36
System.Web.UI.WebControls.TextBox.Render(HtmlTextWriter writer) +32
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,
ControlAdapter adapter) +74
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter
adapter) +291
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +49



All I want to do is get the HTML of a control before it is send to the client.
How do I do this in ASP.NET 2.0?
 
R

Ryan McHale

Is there any solution/workaround to this? Basically the problem is you
cannot render a user control that contains an asp control in it using
the RenderControl method.. If you remove the asp control it works
fine. but then what your rendering is kind of useless.
 
T

trendytim

If you set the hidden (from intellisense, but still accessible) public
property EnableEventValidation on the page object you use to load the
control to false that should fix the problem.

-Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top