Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify the entire ASP.NET page content right before it's output?

I have a page that has a bunch of user controls on it. I want to be able to have "macros" or "placeholders" directly in the content that will get replaced in my code. It shouldn't really matter, but I'm using Ektron as my CMS.

Are there any page events that I can hook into to do a string replace on the entire rendered page content, right before it's sent to the client?

UPDATE

Here is the code that I am currently using to accomplish this:

protected override void Render(HtmlTextWriter writer)
{
    string content = string.Empty;

    using (var stringWriter = new StringWriter())
    using (var htmlWriter = new HtmlTextWriter(stringWriter))
    {
        // render the current page content to our temp writer
        base.Render(htmlWriter);
        htmlWriter.Close();

        // get the content
        content = stringWriter.ToString();
    }

    // replace our placeholders
    string newContent = content.Replace("$placeholder1$", "placeholder1 data").Replace("$placeholder2$", "placeholder2 data");

    // write the new html to the page
    writer.Write(newContent);
}
like image 871
John B Avatar asked Oct 16 '09 19:10

John B


People also ask

Which event is raised after the page loads viewstate for itself and all controls that are included with the request instance after it processes postback data?

Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance. The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded.

What is page lifecycle?

When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering.


2 Answers

There are two approaches you could use:

  1. This is similar to the accepted answer. But I would recommend overriding the render method in a BasePage and deriving all your templates from this.

  2. Use a HttpModule or the Global.asax and attach a Filter to the Response object. To me this make more aesthetic sense because the "Filter" property is supposed to help you filter the output which is exactly what you want!

like image 81
madaboutcode Avatar answered Sep 21 '22 13:09

madaboutcode


Have you looked at the PreRender event in the life-cycle?

Before this event occurs:

• The Page object calls EnsureChildControls for each control and for the page.

• Each data bound control whose DataSourceID property is set calls its DataBind method.

• The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.

I believe this is the last place you could do something like this. The next event is SaveStateComplete, which according to the documentation has this behavior:

Before this event occurs, ViewState has been saved for the page and for all controls. Any changes to the page or controls at this point will be ignored. Use this event perform tasks that require view state to be saved, but that do not make any changes to controls.

Quote source: https://www.c-sharpcorner.com/UploadFile/8911c4/page-life-cycle-with-examples-in-Asp-Net/

like image 44
Aaron Daniels Avatar answered Sep 19 '22 13:09

Aaron Daniels