Why is WebBrowser_DocumentCompleted() firing twice?

前端 未结 9 1215
独厮守ぢ
独厮守ぢ 2020-11-29 08:35

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 webBrow         


        
相关标签:
9条回答
  • 2020-11-29 09:16

    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 讨论(0)
  • 2020-11-29 09:18

    It gets fired once per frame.

    0 讨论(0)
  • 2020-11-29 09:18

    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.

    0 讨论(0)
提交回复
热议问题