1

I want to capture the HTML output of an asp.net page in the master page's LoadComplete event. This is what I have:

public partial class MasterPage_MyBlogMainMaster : System.Web.UI.MasterPage
{
    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        var PageURL = HttpContext.Current.Request.Url.AbsolutePath;

        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);

        HtmlTextWriter hWriter = new HtmlTextWriter(sw);
        base.Render(hWriter);

        string PageResult = sb.ToString();
    }
}

The problem is that the event doesn't seem to trigger. What do I need to change?

Thanks.

frenchie
  • 48,391
  • 102
  • 295
  • 498

2 Answers2

1

Ok, I got it to work; if anyone comes to this page, this is how you do it. The problem is that LoadComplete event needs to be wired in the Page_Load method, like this:

protected void Page_Load(object sender, EventArgs e)
{
     Page.LoadComplete += new EventHandler(Page_LoadComplete);
}

protected void Page_LoadComplete(object sender, EventArgs e)
{
  ... now this works
}
frenchie
  • 48,391
  • 102
  • 295
  • 498
0

Maybe try to use HttpHandler (you can filter out not interesting pages in it also): Capturing HTML generated from ASP.NET

Community
  • 1
  • 1
Konrad Kokosa
  • 16,230
  • 2
  • 35
  • 57