Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
C#
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
Updated 15-Apr-13 21:16pm
v5
Comments
Sergey Alexandrovich Kryukov 16-Apr-13 2:57am    
Did you try correct URI like http://facenama.com?
—SA
Meysam Toluie 16-Apr-13 3:06am    
The above code is not the exact code. It is for explanation.
Yes, I did.

Here is the solution who is interested

C#
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");
 
Share this answer
 
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.
 
Share this answer
 
Comments
Meysam Toluie 16-Apr-13 7:36am    
I handled the DocumentCompleted but i put NO CODE in the handler method and it just updates.
So, the problem is solved, right ?
Meysam Toluie 16-Apr-13 9:14am    
Yes, the solution accepted.
Thanks a lot Meysam Tolouee... :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900