Capturing the HTML from an ASP.NET 2.0 page

  • Thread starter Ken Cox - Microsoft MVP
  • Start date
K

Ken Cox - Microsoft MVP

I'm trying to find a way to program in ASP.NET 2.0 but capture the HTML
output. I found the following routine in ASP.NET 2.0 Cookbook from O'Reilly.
It doesn't work if I include a server-side dropdownlist control on the page.
The error is RegisterForEventValidation can only be called during Render();

Any ideas?

Ken
Microsoft MVP [ASP.NET]


<%@ Page Language="VB" %>

<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Const OUTPUT_FILENAME As String = "renderedpage.html"
Dim renderedOutput As StringBuilder = Nothing
Dim strWriter As IO.StringWriter = Nothing
Dim tWriter As HtmlTextWriter = Nothing
Dim outputStream As IO.FileStream = Nothing
Dim sWriter As IO.StreamWriter = Nothing
Dim filename As String
Dim nextPage As String

Try
'create a HtmlTextWriter to use for rendering the page
renderedOutput = New StringBuilder
strWriter = New IO.StringWriter(renderedOutput)
tWriter = New HtmlTextWriter(strWriter)

'render the page output
Page.RenderControl(tWriter)

'save the rendered output to a file
filename = Server.MapPath(".") & "\" & OUTPUT_FILENAME
outputStream = New IO.FileStream(filename, _
IO.FileMode.Create)
sWriter = New IO.StreamWriter(outputStream)
sWriter.Write(renderedOutput.ToString())
sWriter.Flush()

'redirect to another page
'NOTE: Continuing with the display of this page will result in
the
' page being rendered a second time which will cause an
exception
' to be thrown
nextPage = "DisplayMessage.aspx?" & _
"PageHeader=Information" & "&" & _
"Message1=HTML Output Saved To " & OUTPUT_FILENAME
Response.Redirect(nextPage)

Finally
'clean up
If (Not IsNothing(outputStream)) Then
outputStream.Close()
End If

If (Not IsNothing(tWriter)) Then
tWriter.Close()
End If

If (Not IsNothing(strWriter)) Then
strWriter.Close()
End If
End Try
End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Capture Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="DropDownList1" runat="server">
<asp:listitem>red</asp:listitem>
<asp:listitem>blue</asp:listitem>
<asp:listitem>green</asp:listitem>
</asp:dropdownlist></div>
</form>
</body>
</html>
 
T

Teemu Keiski

I responded already on Forums, but here's for reference
**
Hello,

it complained for me about event validation , so when disabling it with
EnableEventValidation="False" helped. But, if you try it this way e.g moving
the code to overridden Render method

<%@ Page Language="VB" %>

<script runat="server">

Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
Const OUTPUT_FILENAME As String = "renderedpage.html"
Dim renderedOutput As StringBuilder = Nothing
Dim strWriter As IO.StringWriter = Nothing
Dim tWriter As HtmlTextWriter = Nothing
Dim outputStream As IO.FileStream = Nothing
Dim sWriter As IO.StreamWriter = Nothing
Dim filename As String
Dim nextPage As String

Try
'create a HtmlTextWriter to use for rendering the page
renderedOutput = New StringBuilder
strWriter = New IO.StringWriter(renderedOutput)
tWriter = New HtmlTextWriter(strWriter)

MyBase.Render(tWriter)

'save the rendered output to a file
filename = Server.MapPath(".") & "\" & OUTPUT_FILENAME
outputStream = New IO.FileStream(filename, _
IO.FileMode.Create)
sWriter = New IO.StreamWriter(outputStream)
sWriter.Write(renderedOutput.ToString())
sWriter.Flush()

' redirect to another page
' NOTE: Continuing with the display of this page will result in
the
' page being rendered a second time which will cause an
exception
' to be thrown
nextPage = "DisplayMessage.aspx?" & _
"PageHeader=Information" & "&" & _
"Message1=HTML Output Saved To " & OUTPUT_FILENAME
'Response.Redirect(nextPage)
'Response.Write(renderedOutput.ToString())

writer.Write(renderedOutput.ToString())
Finally

'clean up
If (Not IsNothing(outputStream)) Then
outputStream.Close()
End If

If (Not IsNothing(tWriter)) Then
tWriter.Close()
End If

If (Not IsNothing(strWriter)) Then
strWriter.Close()
End If
End Try
End Sub


</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Capture Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="DropDownList1" runat="server">
<asp:listitem>red</asp:listitem>
<asp:listitem>blue</asp:listitem>
<asp:listitem>green</asp:listitem>
</asp:dropdownlist></div>
</form>
</body>

Is that what you are looking for?


Teemu


Ken Cox - Microsoft MVP said:
I'm trying to find a way to program in ASP.NET 2.0 but capture the HTML
output. I found the following routine in ASP.NET 2.0 Cookbook from
O'Reilly. It doesn't work if I include a server-side dropdownlist control
on the page. The error is RegisterForEventValidation can only be called
during Render();

Any ideas?

Ken
Microsoft MVP [ASP.NET]


<%@ Page Language="VB" %>

<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Const OUTPUT_FILENAME As String = "renderedpage.html"
Dim renderedOutput As StringBuilder = Nothing
Dim strWriter As IO.StringWriter = Nothing
Dim tWriter As HtmlTextWriter = Nothing
Dim outputStream As IO.FileStream = Nothing
Dim sWriter As IO.StreamWriter = Nothing
Dim filename As String
Dim nextPage As String

Try
'create a HtmlTextWriter to use for rendering the page
renderedOutput = New StringBuilder
strWriter = New IO.StringWriter(renderedOutput)
tWriter = New HtmlTextWriter(strWriter)

'render the page output
Page.RenderControl(tWriter)

'save the rendered output to a file
filename = Server.MapPath(".") & "\" & OUTPUT_FILENAME
outputStream = New IO.FileStream(filename, _
IO.FileMode.Create)
sWriter = New IO.StreamWriter(outputStream)
sWriter.Write(renderedOutput.ToString())
sWriter.Flush()

'redirect to another page
'NOTE: Continuing with the display of this page will result in
the
' page being rendered a second time which will cause an
exception
' to be thrown
nextPage = "DisplayMessage.aspx?" & _
"PageHeader=Information" & "&" & _
"Message1=HTML Output Saved To " & OUTPUT_FILENAME
Response.Redirect(nextPage)

Finally
'clean up
If (Not IsNothing(outputStream)) Then
outputStream.Close()
End If

If (Not IsNothing(tWriter)) Then
tWriter.Close()
End If

If (Not IsNothing(strWriter)) Then
strWriter.Close()
End If
End Try
End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Capture Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="DropDownList1" runat="server">
<asp:listitem>red</asp:listitem>
<asp:listitem>blue</asp:listitem>
<asp:listitem>green</asp:listitem>
</asp:dropdownlist></div>
</form>
</body>
</html>
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top