Click here to Skip to main content
11,789,512 members (52,111 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WebBrowserControl
Hello Experts
When I Navigate in the Webbrowser the Document property dose not represent the new page document; In another word it doesn't update.

Why it happens? what should I do?

Explanation: In the below code the document1 is equal to document2.
HtmlDocument document1 = this.WebBrowser.Document;
//Here I sign in so the webBrowser navigates to another page

HtmlDocument document2 = this.WebBrowser.Document;
Posted 15-Apr-13 19:42pm
Edited 15-Apr-13 21:16pm
Sergey Alexandrovich Kryukov at 16-Apr-13 2:57am
Did you try correct URI like
Meysam Tolouee at 16-Apr-13 3:06am
The above code is not the exact code. It is for explanation.
Yes, I did.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please refer the below answers.

1. WebBrowser Document is always null[^].
You should handle the DocumentCompleted[^] event and access the document in your event handler when that fires.

Navigation and document loading is handled asynchronously - therefore the control hasn't actually navigated or loaded anything when the Navigate method returns; hence why these are null.

2. .NET C#: WebBrowser control Navigate() does not load targeted URL[^]. Find example here.
Meysam Tolouee at 16-Apr-13 7:36am
I handled the DocumentCompleted but i put NO CODE in the handler method and it just updates.
Tadit Dash at 16-Apr-13 7:48am
So, the problem is solved, right ?
Meysam Tolouee at 16-Apr-13 9:14am
Yes, the solution accepted.
Tadit Dash at 16-Apr-13 9:52am
Thanks a lot Meysam Tolouee... :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here is the solution who is interested

HtmlElement headElement = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptElement = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement domScriptElement = (IHTMLScriptElement)scriptElement.DomElement;
domScriptElement.text = "function applyChanges(){/*DO WHATEVER YOU WANT HERE*/}";
// Call the nextline whenever you want to execute your code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 954
1 Maciej Los 765
2 KrunalRohit 666
3 CPallini 606
4 Richard MacCutchan 420

Advertise | Privacy | Mobile
Web02 | 2.8.1509028.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100