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

5

10

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);
}
Fugleman answered 16/10, 2009 at 19:39 Comment(0)
H
9

Have you tried overriding the render method?

protected override void Render(HtmlTextWriter writer)
{
   StringBuilder htmlString = new StringBuilder(); // this will hold the string
   StringWriter stringWriter = new StringWriter(htmlString);
   HtmlTextWriter tmpWriter = new HtmlTextWriter(stringWriter);
   Page.Render(tmpWriter);
   writer.Flush();

   writer.Write(DoReplaceLogic(htmlString.ToString()););
}
Henrique answered 16/10, 2009 at 19:45 Comment(3)
Yes, this is the right place but is it possible (and wise) to do reset and read the stream?Baksheesh
you're never "resetting" the stream since it hasn't been sent yet. I'm not certain if its wise, no, I just answered the question lolHenrique
I am using this method right now, but will wait to accept the answer to see if there is a better solution. Basically I... Let the page write to a temp buffer. Modify the buffer. Replace the original buffer with the modified one.Fugleman
S
6

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!

Scapegrace answered 8/11, 2009 at 1:21 Comment(1)
Thanks for the answer. Up-vote for effort! The BasePage thing isn't really a different solution, but more of an addition to the accepted one. But good idea. I think it's always a good idea to create a BasePage class that all of your page derive from. Or maybe 2-3 depending on the types of pages you have. As for the HttpModule and filter, that is a great! Again, similar to the accepted solution, but the logic for the replacements would be extracted out of the page. This would be great if you're doing replacements in more than one place! Nice!Fugleman
V
2

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/

Voncile answered 16/10, 2009 at 19:46 Comment(0)
H
0

The simplistic answer that comes to mind is to use asp:Literal controls for your "placeholders". You can set their content during page load, or you can hook into the PreRender event and set them then.

Halliard answered 16/10, 2009 at 19:47 Comment(2)
Unfortunately, I can't do that because these "placeholders" will be inside content that get rendered inside a user control, that I can't touch. So by the time it gets to me, the only thing I could hope to do is look at the rendered content.Fugleman
Sorry, I missed the "user control", my bad. User controls have PreRender events as well. You should be able to put public properties on your user controls that the page can set, then have the user controls do the substitution.Halliard
O
0

It sounds like you might want to have HTML literals within your page and then you can simply replace them with the appropriate content on the Page_Load event.

This will require you to write out HTML code, as opposed to some simple text, but it sounds like you may be injecting your own JavaScript code or the like in there, which this will work great for.

Obidiah answered 16/10, 2009 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.