Retrieving Rendered Source from Page

  • Thread starter Thread starter Ray Booysen
  • Start date Start date
R

Ray Booysen

Hi All

In my ASP.NET project, all my pages inherit from a base class which
extends the Page object. I also have a pdf generator that takes in HTML
Text and will generate a PDF for you. What I want to implement in my
custom Page class is the ability for the page to generate the HTML text
of itself. Can this be done? I have tried looping through all my
controls and rendering them as per your example but am hitting some walls.

Any ideas?

Kind Regards

Ray Booysen
 
Ray said:
Hi All

In my ASP.NET project, all my pages inherit from a base class which
extends the Page object. I also have a pdf generator that takes in HTML
Text and will generate a PDF for you. What I want to implement in my
custom Page class is the ability for the page to generate the HTML text
of itself. Can this be done? I have tried looping through all my
controls and rendering them as per your example but am hitting some walls.

Any ideas?

Kind Regards

Ray Booysen
I also meant to say this is in an ASP.NET 1.1 project.
 
Hey Ray,

Here's something adapted from ASP.NET 2.0 Cookbook from O'Reilly
(http://www.oreilly.com/catalog/aspnetckbk2/)

This works in 2.0. Haven't tried it in 1.1 Maybe it will give you an idea?

Ken
Microsoft MVP [ASP.NET]


<%@ page language="VB" validaterequest="false" enableviewstate="false" %>

<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

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()
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>
</html>
 
Hi Kevin.

I'll give it a shot. Thanks!
Hey Ray,

Here's something adapted from ASP.NET 2.0 Cookbook from O'Reilly
(http://www.oreilly.com/catalog/aspnetckbk2/)

This works in 2.0. Haven't tried it in 1.1 Maybe it will give you an idea?

Ken
Microsoft MVP [ASP.NET]


<%@ page language="VB" validaterequest="false" enableviewstate="false" %>

<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

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()
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>
</html>
 
It's Ken, not Kevin, but let us know how it works for you?

Ken

Ray Booysen said:
Hi Kevin.

I'll give it a shot. Thanks!
Hey Ray,

Here's something adapted from ASP.NET 2.0 Cookbook from O'Reilly
(http://www.oreilly.com/catalog/aspnetckbk2/)

This works in 2.0. Haven't tried it in 1.1 Maybe it will give you an
idea?

Ken
Microsoft MVP [ASP.NET]


<%@ page language="VB" validaterequest="false" enableviewstate="false" %>

<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

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()
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>
</html>



Ray Booysen said:
Hi All

In my ASP.NET project, all my pages inherit from a base class which
extends the Page object. I also have a pdf generator that takes in HTML
Text and will generate a PDF for you. What I want to implement in my
custom Page class is the ability for the page to generate the HTML text
of itself. Can this be done? I have tried looping through all my
controls and rendering them as per your example but am hitting some
walls.

Any ideas?

Kind Regards

Ray Booysen
 
Woops, my mistake... I'll let you know.
It's Ken, not Kevin, but let us know how it works for you?

Ken

Ray Booysen said:
Hi Kevin.

I'll give it a shot. Thanks!
Hey Ray,

Here's something adapted from ASP.NET 2.0 Cookbook from O'Reilly
(http://www.oreilly.com/catalog/aspnetckbk2/)

This works in 2.0. Haven't tried it in 1.1 Maybe it will give you an
idea?

Ken
Microsoft MVP [ASP.NET]


<%@ page language="VB" validaterequest="false" enableviewstate="false" %>

<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

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()
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>
</html>



Hi All

In my ASP.NET project, all my pages inherit from a base class which
extends the Page object. I also have a pdf generator that takes in HTML
Text and will generate a PDF for you. What I want to implement in my
custom Page class is the ability for the page to generate the HTML text
of itself. Can this be done? I have tried looping through all my
controls and rendering them as per your example but am hitting some
walls.

Any ideas?

Kind Regards

Ray Booysen
 
Hi Ken

Thanks, works perfectly.

Now the next step is to find a way to add this functionality to call
when required, not every time the page is rendered. The overhead will
be too much if on every render, the page output is stored in memory or
to a file.

Any ideas?

Regards
Ray
It's Ken, not Kevin, but let us know how it works for you?

Ken

Ray Booysen said:
Hi Kevin.

I'll give it a shot. Thanks!
Hey Ray,

Here's something adapted from ASP.NET 2.0 Cookbook from O'Reilly
(http://www.oreilly.com/catalog/aspnetckbk2/)

This works in 2.0. Haven't tried it in 1.1 Maybe it will give you an
idea?

Ken
Microsoft MVP [ASP.NET]


<%@ page language="VB" validaterequest="false" enableviewstate="false" %>

<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

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()
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>
</html>



Hi All

In my ASP.NET project, all my pages inherit from a base class which
extends the Page object. I also have a pdf generator that takes in HTML
Text and will generate a PDF for you. What I want to implement in my
custom Page class is the ability for the page to generate the HTML text
of itself. Can this be done? I have tried looping through all my
controls and rendering them as per your example but am hitting some
walls.

Any ideas?

Kind Regards

Ray Booysen
 
Back
Top