views:

1511

answers:

1

I have a web page that is being displaying in a winform app using the WebBrowser Control. I need to perform an event when the HTML in the web page changes; however, I cannot find an event that is triggered for situations when the pages is updated through Ajax. The DocumentComplete, FileDownloaded, and ProgressChanged events are not always triggered by Ajax requests. The only other way I can think to solve the issue is to poll the document object and look for changes; however, I don't think that is a very good solution.

Is there another event I am missing that will be triggered on an ajax update or some other way to solve the problem?

I am using C# and .net 2.0

+1  A: 

I have been using a a timer, and just watching for a change in a specific elements content.

Private AJAXTimer As New Timer

Private Sub WaitHandler1(ByVal sender As Object, ByVal e As System.EventArgs)
    'Confirm that your AJAX operation has completed.
    Dim ProgressBar = Browser1.Document.All("progressBar")
    If ProgressBar Is Nothing Then Exit Sub

    If ProgressBar.Style.ToLower.Contains("display: none") Then
        'Stop listening for ticks
        AJAXTimer.Stop()

        'Clear the handler for the tick event so you can reuse the timer.
        RemoveHandler AJAXTimer.Tick, AddressOf CoveragesWait

        'Do what you need to do to the page here...

        'If you will wait for another AJAX event, then set a
        'new handler for your Timer. If you are navigating the
        'page, add a handler to WebBrowser.DocumentComplete
    End If
Exit Sub

Private Function InvokeMember(ByVal FieldName As String, ByVal methodName As String) As Boolean
        Dim Field = Browser1.Document.GetElementById(FieldName)
        If Field Is Nothing Then Return False

        Field.InvokeMember(methodName)

        Return True
    End Function

I have 2 objects that get event handlers, the WebBrowser and the Timer. I rely mostly on the DocumentComplete event on the WebBrowser and the Tick event on the Timer.

I write DocumentComplete or Tick handlers per action required, and each handler will usually RemoveHandler itself, so a successful event will only be handled once. I also have a procedure called RemoveHandlers that will remove all handlers from the browser and timer.

My AJAX commands usually look like:

AddHandler AJAXTimer.Tick, AddressOf WaitHandler1
InvokeMember("ContinueButton", "click")
AJAXTimer.Start

My Navigate commands like:

AddHandler Browser1.DocumentComplete, AddressOf AddSocialDocComp
Browser1.Navigate(NextURL) 'or InvokeMember("ControlName", "click") if working on a form.