开发者

Saving html content via a streamreader?

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?


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
}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜