By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,493 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,493 IT Pros & Developers. It's quick & easy.

get HTML of control in ASP.NET 2.0

P: n/a
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.RegisterForEvent Validation(String
uniqueId, String argument) +239
System.Web.UI.WebControls.TextBox.AddAttributesToR ender(HtmlTextWriter
writer) +2370
System.Web.UI.WebControls.WebControl.RenderBeginTa g(HtmlTextWriter
writer) +36
System.Web.UI.WebControls.TextBox.Render(HtmlTextW riter writer) +32
System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter 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?
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

Nov 19 '05 #2

P: n/a
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

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.