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 RegisterForEven tValidation 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.EventArg s) Handles Me.Load
Const OUTPUT_FILENAME As String = "renderedpage.h tml"
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.RenderCont rol(tWriter)
'save the rendered output to a file
filename = Server.MapPath( ".") & "\" & OUTPUT_FILENAME
outputStream = New IO.FileStream(f ilename, _
IO.FileMode.Cre ate)
sWriter = New IO.StreamWriter (outputStream)
sWriter.Write(r enderedOutput.T oString())
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=Inf ormation" & "&" & _
"Message1=H TML Output Saved To " & OUTPUT_FILENAME
Response.Redire ct(nextPage)
Finally
'clean up
If (Not IsNothing(outpu tStream)) Then
outputStream.Cl ose()
End If
If (Not IsNothing(tWrit er)) Then
tWriter.Close()
End If
If (Not IsNothing(strWr iter)) Then
strWriter.Close ()
End If
End Try
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Captur e Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownli st id="DropDownLis t1" runat="server">
<asp:listitem>r ed</asp:listitem>
<asp:listitem>b lue</asp:listitem>
<asp:listitem>g reen</asp:listitem>
</asp:dropdownlis t></div>
</form>
</body>
</html>