Click here to Skip to main content
Rate this: bad
good
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.
this.WebBrowser.Navigate("facenama.com");
 
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 20:42pm
Edited 15-Apr-13 22:16pm
v5
Comments
Sergey Alexandrovich Kryukov at 16-Apr-13 2:57am
   
Did you try correct URI like http://facenama.com?
—SA
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
good
Please Sign up or sign in to vote.

Solution 1

Please refer the below answers.
 
1. WebBrowser Document is always null[^].
Quote:
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.
Comments
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
good
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*/}";
headElement.AppendChild(scriptElement);
 
// Call the nextline whenever you want to execute your code
webBrowser1.Document.InvokeScript("applyChanges");
  Permalink  

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

  Print Answers RSS
0 Mathew Soji 274
1 BillWoodruff 270
2 Afzaal Ahmad Zeeshan 244
3 Sergey Alexandrovich Kryukov 240
4 Raul Iloc 160
0 OriginalGriff 6,219
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,103
3 Manas Bhardwaj 4,549
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
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