views:

31

answers:

3

I want to change some elements text when page is leaving the server (page_render, endRequest etc.).

How can i get access to the page and how can i find the elements to change their values, texts?

+1  A: 

You can do so by using a HttpModule. This sits in the pipeline and can do pre- and postprocessing.

For example take a look at this whitespaceremover.

XIII
For others finding this: though it's a valid example *please* don't actually use the linked module...it does more harm than good, and saves *very* little bandwidth since you *should* be delivering pages gzipped anyway :)
Nick Craver
+1  A: 

Besides HttpModules, you can also override the 'Render' method (or do this in a basepage to make it reusable).

protected override void Render(HtmlTextWriter writer )
{
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

    base.Render(htmlWriter);

    string html = stringWriter.ToString();

    // do stuff with the html

    writer.Write(html);
} 
Jan Jongboom
+1  A: 

There are a number of options and which one suites you will depend largely on what the actual goal is.

  1. Handle the PreRender event of a Page and adjust any elements you want to in this event. Ideally you would put this in a base class that is inherited by all the pages that require this processing. This gives you access to the actual page model and control tree.
  2. Setup a filter that will give you direct access to the response stream. You can implement this in 2 ways, either as a separate HttpModule that installs the filter or you can install the filter directly from the Global.asax. Which route you choose depends on how reusable you need this, with the HttpModule being the most reusable.

Here is a nice article Modifying the HTTP Response Using Filters

Chris Taylor