473,548 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Capturing the HTML from an ASP.NET 2.0 page

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>
Feb 12 '06 #1
2 1711
I responded already on Forums, but here's for reference
**
Hello,

it complained for me about event validation , so when disabling it with
EnableEventVali dation="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.H tmlTextWriter)
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)

MyBase.Render(t Writer)

'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.Redir ect(nextPage)
'Response.Write (renderedOutput .ToString())

writer.Write(re nderedOutput.To String())
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 id="Head1" 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>

Is that what you are looking for?
Teemu
"Ken Cox - Microsoft MVP" <BA**********@h otmail.com> wrote in message
news:ei******** ********@TK2MSF TNGP10.phx.gbl. ..
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>

Feb 12 '06 #2
Thanks Teemu! That's perfect!

Ken

"Teemu Keiski" <jo****@aspalli ance.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I responded already on Forums, but here's for reference
**
Hello,

it complained for me about event validation , so when disabling it with
EnableEventVali dation="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.H tmlTextWriter)
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)

MyBase.Render(t Writer)

'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.Redir ect(nextPage)
'Response.Write (renderedOutput .ToString())

writer.Write(re nderedOutput.To String())
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 id="Head1" 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>

Is that what you are looking for?
Teemu

Feb 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1711
by: Dan | last post by:
A Newbie needs help! Is it possible, in PHP, to capture the resulting HTML of a submitted form returning from a server and incorporate that HTML into say a table, or some predefined layout? I am trying to incorporate the resulting HTML into a layout I have created in order to keep the look & feel of my site consistent, but the resulting...
4
4862
by: Dr. StrangeDub | last post by:
I am looking for a way to capture the HTML file generated by an ASP.Net application (just as is sent back to the client) and save it to a designated spot on the web server. Here's a bit of background: our application has a Report function that dynamically creates a new page containing various tables of real-time data (through the C# code...
6
15593
by: Robert Nurse | last post by:
Hi, Is it at all possible to capture and suppress the backspace key when focus is not on a form edit control? Basically, while the page is loading I don't want the user pressing the backspace key which IE interprets as a desire to return to the previous page. Thanks
15
2001
by: Larry Asher | last post by:
Hi all. I'm a bit of a novice in this arena so please forgive if this question reflects that. I am trying to grab the html from a website and display it within another webpage (once I get this to work I am going to manipulate the html in other ways - this isn't the end purpose of this effort). To do this I am trying to open another window...
4
2108
by: Sandy Bremmer | last post by:
I am wondering if the following can be accomplished with javascript (and if so, if you think javascript is an appropriate solution). I'm afraid my javascript skills lack. I need to build a hyperlink that includes the current web page URI. When clicked, the current page is then processed by a program. For example, it would look something...
33
5577
by: Joerg Schuster | last post by:
Hello, Python regular expressions must not have more than 100 capturing groups. The source code responsible for this reads as follows: # XXX: <fl> get rid of this limitation! if p.pattern.groups > 100: raise AssertionError( "sorry, but this version only supports 100 named groups"
4
3209
by: PJ | last post by:
I would like to capture a request stream before the request has been completely sent to the server for the sake of saving the bytes of a posted file to disk. I have written httpmodules before, but (correct me if I am wrong) it seems that by the ProcessRequest event, the Request has been completely sent and aspnet_wp has used it to hydrate the...
2
6397
by: Andrew | last post by:
Hi, I have a problem capturing the checkboxes that are checked, I get false irrespective of wether they are checked or not. I have gone thru the sample code on this forum, but they dun seem to work. This is the code that I used to go thru the repeater control to find my checkboxes. foreach(RepeaterItem r in MyRepeater.Items)
10
5998
by: Andrew | last post by:
Hi, I have a messagebox that pops up due to an event. I did it in javascript. ie. alert("Time's up. Assessment Ended"); I want to capture the OK and Cancel events of this alert messagebox. My code is in C#/ASP.NET. TIA. Andrew.
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5367
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.