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 :

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);
// *** store to a string
string PageResult = sb.ToString();
// *** Write it back to the server

No comments:

Post a Comment