tags:

views:

410

answers:

5

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

+1  A: 

You can use a HTTPModule to change the html. Here is a sample.

Hope it helps, Bruno Figueiredo http://www.brunofigueiredo.com

Bruno Shine
Dude, don't shamelessly self-promote your website - it's already in your profile, leave it at that.
Jason Bunting
A: 

Take a look at the sequence of events in the ASP.NET page's lifecycle. Here's one page that lists the events. It's possible you could find an event to handle that's late enough in the page's lifecycle to make your changes, but still get those changes rendered.

If not, you could always write an HttpModule that processes the HTTP response after the page itself has finished rendering.

Martin
A: 

I don't think there is a specific event from the page that you can hook into; here is the ASP.Net lifecycle: http://msdn.microsoft.com/en-us/library/ms178472.aspx

You may want to consider hooking into the prerender event to 'adjust' the values of the controls, or perform some client side edits/callbacks.

Brian Schmitt
A: 

Obviously it will be much more efficient if you can coax the desired markup out of ASP.Net in the first place.

With that in mind, have you considered using Control Adapters? They will allow you to over-ride how each of your controls render in the first place, rather than having to modify the string later.

Joel Coehoorn
+4  A: 

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);
}
korchev