18

Well, I'm using a simple webbrowser control to browse to a page, so I need to change the Text of the form while doing so. I'm using -

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
     this.Text += " - " + webBrowser1.Document.Domain;
}

but using a breakpoint, i noticed that, this event is firing twice. I even tried _Navigated() event. it also fired twice. Resulting the title to "Webber - google.co.in - google.co.in" ..

I also noticed that this event fired several times while loading msn.com.. I'm trying to change the text of the form only when the page has finished loading totally..

Any remedy?

0

9 Answers 9

28

You can check the WebBrowser.ReadyState when the event is fired:

if (browser.ReadyState != WebBrowserReadyState.Complete)
    return;

ReadyState will be set to Complete once the whole document is ready.

1
  • 1
    it is most likely firing again for any external javascript links in the file Commented Aug 21, 2012 at 11:03
18

Every time a frame loads, the event is fired.

Also, before you even go there, the IsBusy property will only be True whilst the first frame has not loaded.

void BrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
    return; 

  //The page is finished loading 
}
2
  • 1
    I fear to ask, but did you link it up to the event? All it is, is the document completed after all. Commented Feb 25, 2010 at 19:37
  • Thanks! Exactly what I needed - couldn't figure out why some pages were rendering completely, while others were only partially rendered.
    – Elie
    Commented Dec 28, 2012 at 4:05
5

I have the same problem, and the reason was because, by default when you add the control it generate designer code like this.

this.webBrowser1.Url =  new System.Uri("", System.UriKind.Relative);

and if you change the url after calling

InitializeComponent();
WebBrowser.Navigate("NewUrl.com");

It will load two different pages: About:Blank and NewUrl.com

Just, remove the designer code... and you'll stop the "double" event.

1
  • I bumped into the same issue and this workaround worked fine to me. I'm a little bit surprised the Url properties seems to be "enqueued" somehow : the DocumentCompleted triggers with e.Url == "about:blank" even if I hook it (+=) after a Url = "http://..." declaration
    – Larry
    Commented Apr 15, 2011 at 9:40
3

It gets fired once per frame.

3

If firing twice is a problem then this should work:

  string body="";

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (body == webBrowser1.Document.Body.InnerHtml) return;
        body = webBrowser1.Document.Body.InnerHtml;

        // Here is something you want
    }
1
  • it will so much time, if content between body is so much. Commented Apr 22, 2021 at 8:57
1

Might be you are subscribing this event multiple times like in your some method when your navigating to URL everytime you subscribe to this event.

To solve this problem, move that line out of the method and put it somewhere else where it will only be called once per instance. In the constructor of the class perhaps... That should solve your problem .

0

Actually, it doesn't always get fired. Haven't figured out why not. I have a timer and just check the ReadyState repeatedly for a few minutes. (Using embedded browser control).

0

if (browser.ReadyState != WebBrowserReadyState.Complete) is recommended.

And when there are frames in the page,DocumentCompleted will be fired several times.And this is difficult to solve.Some ways like checking the urls are not accurate.

BTW, why not using this:

this.Text = stringA + " - " + webBrowser1.Document.Domain;

Try to using a fixed prefix,problem may be solved easily.

0

How To Determine When a Page Is Done Loading in WebBrowser Control DocumentCompleted is WinForms' wrapper of the DocumentComplete evert, however WebBrowserDocumentCompletedEventArgs hides the sender parameter so you cannot tell which frame is raising the event. Alternatively you can check WebBrowser.ReadyState.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.