0

Is there a way to save html content of an aspx page in the pageload part of the cs file and have it loaded again on postback?

Maybe using a streamreader to save it then have the streamreader write the content back in?

If so does anyone have any examples?

G Gr
  • 6,030
  • 20
  • 91
  • 184

3 Answers3

1

ASP.NET has an extensive caching mechanism which is meant to do what you describe

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
1

Something along these lines HtmlTextWriter to String - Am I overlooking something? can be done. I've done it with the Render() method of the page, not the RenderContents method. I can't for the life of me remember why I did that, though. It may have been for versions of ASP.net before they introduced the ability to cache most of a page, except for small pieces. Unless you really need to do this, use the built in caching functionality.

Community
  • 1
  • 1
joelt
  • 2,672
  • 2
  • 24
  • 32
  • ok so now reading up on cacheing im LIKE WOW why dont they ever teach us about these things in school! But how would I store a cached page in mysql? – G Gr Mar 16 '11 at 01:49
  • (so the content is never lost) – G Gr Mar 16 '11 at 01:49
  • Do you really care that much that the content is never lost? Are you doing some super-logging type thing? – joelt Mar 16 '11 at 02:06
  • well im creating my own social networking page, it wouldnt be very good if a user logged in and found he had no wall posts (similar to facebook) – G Gr Mar 16 '11 at 02:11
  • 1
    OK, you don't want to store the entire page in the database. You want to store individual posts in the database. – joelt Mar 16 '11 at 02:26
  • Garrith Graham: yep, you don't really want to store HTML pages in the database. You need to store individual posts as joelt said. then when user goes back to the page you render this page with all active elements (for example 'edit post' functionality) and you insert the saved individual post bodies in the page. – Andrew Savinykh Mar 16 '11 at 04:30
  • And for you purposes you don't need asp.net caching either. Caching is good for something entirely different. – Andrew Savinykh Mar 16 '11 at 04:32
1

Do you mean something like this, capturing the generated HTML by overriding the Render method?

protected override void Render(HtmlTextWriter writer)
{
    string pageSource;

    // setup a TextWriter to capture the markup
    using (var sw = new StringWriter())
    using (var htw = new HtmlTextWriter(sw))
    {
        // render the markup into our surrogate TextWriter
        base.Render(htw);

        // get the captured markup
        pageSource = sw.ToString();
    }

    // render the markup into the output stream
    writer.Write(pageSource);

    // now you can do what you like with the captured markup in pageSource
}
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • @Garrith: It's just a string. You'd need to persist it somewhere in order to be able to get at it again later: in session state, in application state, in a database, on disc, wherever you like. – LukeH Mar 16 '11 at 02:05
  • o sorry thats what i mean, if im retrieveing it from mysql, how is the html content redisplayed back on to the users profile? – G Gr Mar 16 '11 at 02:06
  • @Garrith Graham: you can chuck it in a literal control, but if you have any active content inside (i.e. asp.net controls, etc) these are not going to work when the page is rendered. – Andrew Savinykh Mar 16 '11 at 04:27
  • well its contained in an content page and the only controls on the wall post are html controls (textarea and html button) can it still be done? – G Gr Mar 16 '11 at 13:05