About 6 months ago my Adsense Alert program stopped working, basically because Google made a load of changes to their site including a huge amount of Ajax. The trouble was that I couldn’t get the WebBrowser component to populate with the full page source after all the Ajax calls had been completed. It wasn’t a problem I worked on constantly but instead just dipped in and out of trying various ways of waiting for the page load to complete and/or forcing the document within the web browser control to refresh. Nothing worked, then yesterday determined to sort it out once and for all I had a brainwave and have now solved the problem.
The trick to getting the .net WebBrowser control to give you the full page source is to attach an onpropertychange event handler to the Div element that the Ajax is updating.
It’s as simple as doing something like the following:-
HtmlElement htmlUpdated = webBrowser1.Document.GetElementById("div-that-is-updated");
htmlUpdated.AttachEventHandler("onpropertychange", new EventHandler(UpdatedHandler));
When the div-that-is-updated element in the document is updated it fires the UpdatedHandler method, example below:-
private void UpdatedHandler(Object sender, EventArgs e)
HtmlElement htmlDivThatIsUpdated = webBrowser1.Document.GetElementById("div-that-is-updated");
string strFullSource = htmlDivThatIsUpdated.InnerHtml;
strFullSource now contains the complete HTML for that Div element after it has been updated. It’s really simple but works perfectly, I have no idea why i hadn’t thought of this earlier. Once I get around to to updating the install I’ll have a new working version of Adsense Alert out in the next few days.