Nikola Stjelja Nikola Stjelja - 1 month ago 12
HTML Question

ASP.NET modifing the HTML of a page before it is sent to the client

I need to catch the HTML of a ASP.NET just before it is being sent to the client in order to do last minute string manipulations on it, and then send the modified version to the client.

E.G.

The Page is loaded
Every control has been rendered correctly
The Full html of the page is ready to be transfered back to the client

Is there a way to that in ASP.NET

Answer Source

You can override the Render method of your page. Then call the base implementation and supply your HtmlTextWriter object. Here is an example

protected override void Render(HtmlTextWriter writer)
{
    StringWriter output = new StringWriter();
    base.Render(new HtmlTextWriter(output));
    //This is the rendered HTML of your page. Feel free to manipulate it.
    string outputAsString = output.ToString();

    writer.Write(outputAsString);
}