ASP.NET, Programming, VB.NET, Web Development

ASP.NET Webforms: How to Save the HTML Sent to the Browser

Save My Web Page

Often when building a Web site there is a need to log the HTML of the page that is displayed to the user. This, for example, can be useful for history logging or emailing a copy of the form.

Since I frequently use .NET Webforms it is actually relatively easy to obtain the rendered HTML and store it for future use. Surprisingly there is relatively little information online about how to do this.

How is this done? The key is the page’s Render event. I add a method that overrides the page’s Render event and records the stream as it is sent to the browser. Once the page has been streamed and recorded the result can be saved to the database or stored in the session as you see fit.

Below I will give an example in VB.NET, but the principal is the same in C#:

 
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        MyBase.Render(htw)
        Dim thisPage As String = sb.ToString()
        writer.Write(thisPage)
        Session("abc") = thisPage
End Sub
Advertisements

3 thoughts on “ASP.NET Webforms: How to Save the HTML Sent to the Browser”

  1. This is such an excellent idea. To be able to store/record the page as the user experienced it at a given time can be very useful.

    What did MyBase.Render(htw) render?

    1. It’s true, this functionality could be useful in so many different ways! Right now it’s great for reporting if I want to stream a report that the user just viewed to Excel and run any manipulations on it to make it look good for Excel.

      But another great function would be to take snapshots of forms as before/after images stored in the database to document what a user did. This could be quite useful if users ask for support on forms or wish to see historical information.

      To answer your question about MyBase.Render: the Render Sub is overriding the default Page Render, and MyBase.Render takes the HtmlTextWriter htw as an argument. So when the Page is rendered to the browser the HTML sent is captured and saved via the HtmlTextWriter to sb, our StringBuilder object. This is handy because once the page has rendered we can take the populated StringBuilder and save it to a session variable so that we have a saved instance of the Page that was sent to the user that we can use later to use in any way we want… to save to a historical table, or even manipulate for a report.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s