How do you check for ajax updates using a WebBrowser control in .net 2.0?

I have a webpage that is displayed in a winform application using a WebBrowser control. I need to execute an event when the HTML on the web page changes; however, I cannot find an event that fires for situations when pages are refreshed through Ajax. DocumentComplete, FileDownloaded, and ProgressChanged events are not always triggered using Ajax requests. The only way I can solve the problem is to poll the document object and look for changes; however, I do not think this is a very good solution.

Is there another event that I am missing that will be fired when ajax is updated or in any other way to solve the problem?

I am using C # and .net 2.0

+3
source share
1 answer

I use a timer and just watch the contents of certain elements change.

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, WebBrowser and Timer. I rely mainly on the DocumentComplete event in WebBrowser and the Tick event on the timer.

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

My AJAX commands usually look like this:

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

My navigation commands:

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

Source: https://habr.com/ru/post/1699389/


All Articles