Click here to Skip to main content
11,580,000 members (76,294 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)

Advertise | Privacy | Mobile
Web03 | 2.8.150603.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