Friday, July 01, 2005

Capturing Page-output in ASPX pages

There is a cool trick thru which one can capture the page-output of Aspx pages before it is send to the browser. An excellent article explaining this is at :
http://west-wind.com/weblog/posts/481.aspx

The trick is to override the Page.Render() method, and capture the output in a TextWriter. Then write the same context back to the original textwriter. Sounds confusing..It is a bit :)
Here is the code snippet:

protected override void Render(HtmlTextWriter writer)
{
// *** Write the HTML into this string builder
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hWriter = new HtmlTextWriter(sw);
base.Render(hWriter);
// *** store to a string
string PageResult = sb.ToString();
// *** Write it back to the server
writer.Write(PageResult);
}

No comments:

Post a Comment