How to render a page’s HTML?


I’ve been trying to get the Html of a gridview and in the process I found the way to get the Html of an aspx page @ Microsoft .NET Framework Group at Google. I’ve tested the code and now I’m trying to modify it to get the HTML of a gridview. Wish me luck!

 

<%@ 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>
</html>

 

———————————————————————————-

C# Lovers! dont be disapointed, here’s the code in C# too:-

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
    const string OUTPUT_FILENAME = “renderedpage.html”;
    StringBuilder renderedOutput = null;
    IO.StringWriter strWriter = null;
    HtmlTextWriter tWriter = null;
    IO.FileStream outputStream = null;
    IO.StreamWriter sWriter = null;
    string filename;
    string nextPage;
   
   
    try {
        //create a HtmlTextWriter to use for rendering the page
        renderedOutput = new StringBuilder();
        strWriter = new IO.StringWriter(renderedOutput);
        tWriter = new HtmlTextWriter(strWriter);
       
       
        base.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 (((outputStream != null))) {
            outputStream.Close();
        }
       
       
        if (((tWriter != null))) {
            tWriter.Close();
        }
       
       
        if (((strWriter != null))) {
            strWriter.Close();
        }
    }
}

Advertisements

Author: Imran Akram

A .NET/SharePoint Consultant and a political enthusiast who believes in putting the feet of the elected and/or unelected officials to fire in order to get good governance.

2 thoughts on “How to render a page’s HTML?”

  1. well, actually I tried it this way but it wasn’t working, but I succeeded in doing it another way by getting the html of the whole page with just the gridview and some other labels etc and succeeded. I redirected the control to that page by using Server.Transfer() , so that the user doesnt notice the change in URL and at the end of its page_load I redirected it again to another page (after sending the email) to the page after that… Moral of the story: All is well that ends well. 🙂

    Thanks for the comment, I appreciate this

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