Setting HTML for WebBrowser in C#
Today I tryed to insert HTML for WebBorwser
in C#. I thought it’s a simple task. But when I tryed to use DocumentText
property I got “”. Then I tryed Document.Write("...")
and I got that Document
is null
. After these tries I started to find out the easiest way to fill the WebBrowser with HTML without writing to disk and navigating the written file. Here is my code:
WebBrowser browser = new WebBrowser(); browser.Navigate("about:blank"); browser.Document.Write("..."); |
When the WebBrowser
has been made, the Document
object is null
. So, we should call Navigate()
method to get Document
available.
If You have Your way to fill the WebBrowser
with HTML I’m expecting for Your comments
You just have to wait a little for the document… try inserting, between navigate and write:
while (browser.Document == null && browser.Document.Body == null) Application.DoEvents();
You can also set the html using browser.Document.Body.InnerHTML
wb.AllowNavigation=true
wb.DocumentText=”";
I learn the best way to do it:
webBrowser1.Document.OpenNew(true); // Reset to new document, if don't do this the webBrowser1.Document.Write() just append more html without clear the previously code, and I learn (by the bad way) this duplicates attached events.
webBrowser1.Navigate("about:blank");
while (webBrowser1.Document == null && webBrowser1.Document.Body == null)
Application.DoEvents(); // thanks to 'laurian'
webBrowser1.Document.Write(this.Selected_Html);
Greetings from Monterrey, M
Thanks Zeta. Solved my problem
This helped me solving such a problem:
webBrowser1.Navigate( “http://40″ );
and laurian: it should be || not &&