Capturing Page-output in ASPX pages

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 builderStringBuilder sb = new StringBuilder();StringWriter sw = new StringWriter(sb);HtmlTextWriter hWriter = new HtmlTextWriter(sw);base.Render(hWriter);// *** store to a stringstring PageResult = sb.ToString();// *** Write it back to the serverwriter.Write(PageResult);}