Geeks With Blogs
Paul's development blog Tidbits from Smart Client, Windows Forms, C#, .NET 2.0, XML, and related toys...
It appears that the Windows Forms webbrowser control that shipped with VS2005 and the .NET framework 2.0 has an issue that prevents setting the .DocumentText property. For me, the control behaves most consistenly when I navigate to a blank page, write to the document, and then manipulate the DocumentText, such as in this code:

this.webBrowser1.Navigate("about:blank");
HtmlDocument doc = this.webBrowser1.Document;
doc.Write(
string.Empty);
this.webBrowser1.DocumentText = xmlOutput;

Posted on Monday, December 12, 2005 10:30 PM | Back to top


Comments on this post: C# 2.0 WebBrowser control - bug in DocumentText?

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I noticed the same problem few days back. But it seems that the controls only allows to set DocumentText once and then it ignores any futher assignments. It also doeasn't update the value if you change the document. I wonder if same applies to the DocumentStream?
Left by Szymon on Dec 13, 2005 9:54 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Btw. I'm using this control as HTML editor by settings Contenteditable attribute on Body. But I don't have time to add toolbars and other commands. Do you know of any decent HTML editor control for Windows Forms?
Left by Szymon on Dec 13, 2005 9:57 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I am having the same problem you had with sequential DocumentText assignments in a WebBrowser control. Have you run into any solutions or workarounds?
Left by David on Dec 22, 2005 2:31 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
There doesn't appear to be a way other than to capture the document text to a temporary variable and redo the process I outlined. In essence, the property seems to become ReadOnly after you set it once, but going through the process to navigate to a new page and write to a new document seems like it would mimic the behavior you're looking for.
Left by Paul Whitaker on Dec 22, 2005 2:32 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Set AllowNavigation to true, then you can assign to DocumentText as much as you want.
Left by Sally on Jan 11, 2006 8:14 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
doesnt help me in my VSTO application
Left by Ishai on Feb 14, 2006 3:01 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
AllowNavigation is true, and I am not setting DocumentText at all. I am actually trying to read DocumentText but it is null even though the page is displayed correctly. Does anyone have any ideas?
Left by Dylan on Mar 07, 2006 8:13 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Easiest way I was able to do this was just to copy the code from the designer.cs and make a call to recreate the control each time before setting the DocumentText:

private void ReCreateWebBrowser()
{
this.webBrowserCCRView.Dispose();

this.webBrowserCCRView = new System.Windows.Forms.WebBrowser();
// webBrowserCCRView
//
this.webBrowserCCRView.AllowWebBrowserDrop = false;
this.webBrowserCCRView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.webBrowserCCRView.IsWebBrowserContextMenuEnabled = false;
this.webBrowserCCRView.Location = new System.Drawing.Point(0, 29);
this.webBrowserCCRView.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowserCCRView.Name = "webBrowserCCRView";
this.webBrowserCCRView.Size = new System.Drawing.Size(610, 261);
this.webBrowserCCRView.TabIndex = 1;
this.webBrowserCCRView.TabStop = false;
//
this.Controls.Add(this.webBrowserCCRView);

}
Left by Mike Westemeir on Apr 27, 2006 11:02 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I have been experiencing this problem and thought I had solved it by following the suggestion posted at the Derrick Fribilla URL above. That didn't work for me.

I have confirmed that my AllowNavigation property is set to true, I have confirmed that I never attempt to assign a value to DocumentText in the form's constructor. None of this mattered for me.

The only thing that seems to *consistently* work is if I first Navigate to about:blank, write an empty string to the document and then assign a new value to DocumentText
Left by Terri Policy on May 05, 2006 6:02 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Have anyone solve problem with DocumentText ?
Left by Seba on Jun 12, 2006 9:21 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Seba, the code I listed worked for me. There were a few other suggestions here as well, but I haven't had a chance to test them.
Left by Paul Whitaker on Jun 12, 2006 11:07 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I've tried all the above techniques without success, including the control replacement. Why is this such a bugger?!

Any progress by anyone?
Left by Rob Cecil on Jun 12, 2006 10:56 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Can someone post some working code snippets?

I've tried the replacement code from above to no avail. I tried replacing it in the setter of my class for DocumentText property on the contained WebBrowser instance. After it replaces it, Edit mode doesn't seem to work.

I've tried Document.ExecuteCommand("EditMode", true, null) and

DocumentText = "<HTML><BODY contentEditable='true'></BODY></HTML>"; as indicated by:

http://blogs.msdn.com/winformsue/archive/2006/03/29/564211.aspx#comments

Without success.


Thanks!
Left by Rob Cecil on Jun 13, 2006 1:09 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Probably not the cleanest/best way to do this, but it's working for me so far. Note that this is for when you want to overwrite what's there in its entirety. The document object is created after the DocumentText is set the first time, and after that making a new page and writing to it seems to work fine.

if (webBrowser1.Document != null)
{
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("New HTML Code");
}
else
{
webBrowser1.DocumentText = "HTML Code";
}
Left by James Booze on Oct 10, 2006 4:19 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I have the following problem:
I use the DOM model to update the document contents and I want to get the resulting html now.

But after the Document is being changed, the DocumentText property doesn't seem to be updated. How can I export the COMPLETE html of the document?
Left by Maverick on Oct 20, 2006 9:53 PM

# Winforms: web browser control trouble
Requesting Gravatar...
The last couple of days I had a big fight with the web browser control that ships with Visual Studio
Left by TryParse(this) on Jan 07, 2007 2:49 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I have found the solution. Use the following the get the lastest source than use James Booze solution.


String lastsource = ((mshtml.HTMLDocumentClass)(((webBrowser1.Document.DomDocument)))).documentElement.innerHTML;
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write(lastsource);
Left by Mesut Sahin Boydas on Feb 16, 2007 6:59 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
man, this is a really stupid problem... Here how I solved it.

The main reason why it doesn't works is if your document property 'AllowNavigation' is false, the property 'DocumentText' can only be set for initialization. So, if you want to update it, the value needs to be set to true. Another interesting thing to know is that the operation of setting the 'DocumentText' is asyncronuous. So, if you do something like:
1 - myControl.AllowNavigation = true;
2 - muControl.DocumentText = "my new string";
3 - myControl.AllowNavigation = false;
it won't works.

so instead of doing the step 3 right away, why not using the event 'DocumentCompleted'. This event is called when the document has finished updating the document. So, setting the value to false there seems to works perfectly for me.

I don't know if there is a better way to do that, but for now it should be enouph for me.
Left by AngeDeLaMort on Feb 23, 2007 3:28 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
if (webBrowser1.Document != null)
{
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("New HTML Code");
}
else
{
webBrowser1.DocumentText = "HTML Code";
}


works for me.

AllowNavigation has been set to true.
Left by James Chai on Feb 23, 2007 7:31 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
A work-around to this issue is to create a temp file on disk with the HTML or XML in it that you want and then use the control's .Navigate method to hit the file. Pretty lame I know, but that is what we are doing.

I have tried just about all of the other solutions above to no avail.

Here is the sample code:

StreamWriter fileWriter;
fileWriter = File.CreateText("testfile.xml");
fileWriter.Write("<xml><test>hi!</test></xml>");
fileWriter.Close();
webBrowser1.Navigate("testfile.xml");
Left by Andrew on Feb 27, 2007 10:17 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Hi guys,

There seems to be a lot of a problems with this, I have fixed the problem. Where you want to set the code add the following block of code

'Generate the HTML output
oHtmlProvider.RefreshDataSource(oBankDataTable, oMoneyDataTable)
While WebBrowser1.DocumentText <> oHtmlProvider.HTML
WebBrowser1.AllowNavigation = True
WebBrowser1.DocumentText = oHtmlProvider.HTML
Application.DoEvents()
End While

This should do the trick.
Left by Rob Heeley on Mar 01, 2007 12:47 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
This simple code snippet works fine, try it out,

// With all default properties of webBrowser1

int i;
public void WriteToWebBrowser(){

webBrowser1.AllowNavigation = false; // this, apparently, doesnt have any singificance, i wonder why!!

if (webBrowser1.Document != null)
webBrowser1.Document.OpenNew(true);
else
webBrowser1.Navigate("about:blank");

webBrowser1.Document.Write("New Content "+(i++)+"");
}

Thanx to James Booze, and all of you;
Left by Galib Anwar on Jul 02, 2007 1:03 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
thanks Booze, your trick of

Document.OpenNew(true)

works for me

Left by walty on Jul 02, 2007 11:46 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...

..
Left by = on Oct 17, 2007 1:42 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Instead of

myControl.AllowNavigation = true;
myControl.DocumentText = "my new string";
myControl.AllowNavigation = false;
-> does not work

use

myControl.AllowNavigation = true;
myControl.DocumentText = "my new string";
Application.DoEvents();
myControl.AllowNavigation = false;
-> That works for me!


Curiously another order does not work:

myControl.AllowNavigation = true;
myControl.DocumentText = "my new string";
myControl.AllowNavigation = false;
Application.DoEvents();
-> does not work
Left by Martin Kissel on Oct 22, 2007 12:11 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
This very simple trick worked for me:

if (webBrowser1.Document != null)
webBrowser1.Document.Write(txtDescription.Text);
webBrowser1.DocumentText = txtDescription.Text;
Left by Josh Comley on Nov 08, 2007 10:28 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I had experienced all of this as well, but my solution was much simpler:

webBrowser.Document.Clear();
webBrowser.Document.Write( myHTML );
webBrowser.Document.Close();
Left by Michael T on Dec 03, 2007 11:05 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Use the Navigated event!
Left by hans on Feb 27, 2008 11:07 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
read MSDN CAREFULLY!
Once you assign the DocumentText you trigger a Navigation. After the DocumentCompleted then you can do another assignment!
Left by gka on Mar 19, 2008 4:43 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Hi,

Here how I do it. I prepare html document by assigning html text to some String variable such as

String doc = String.Format("<html><body></body></html>"

Then I use the following code:
if (webBrowser1.Document == null)
{
webBrowser1.DocumentText = "<html><body></body></html>";
}
webBrowser1.Document.OpenNew(true);
webBrowser1.DocumentText="<html><body>"+doc+"</body></html>";
Left by Mat on May 10, 2008 7:39 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Hi everyone.

I'm a developer from a small company. I'm using standard C# WebBrowser component in my project for scraping purposes. It didn't get problem when using it with proxy. But suddenly, previous week I switched off proxy on my PC. The app worked very well. But this week I switched on proxy on my PC. Now the app is not working. What should I do? Please help me!
Left by Max on May 20, 2008 6:22 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...






Left by a on May 21, 2008 3:06 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I have finally figured out what is causing this issue.

I expect everyone here who has experienced problems has an event that will also set the DocumentText. Well that is what I had; a checkbox checkedChanged event.

The problem is caused when the DocumentText is being set twice or more without displaying the new HTML in between the sets. For some reason when we set the data twice without displaying the content of the first set, the WebBrowser control just displays blank HTML ("<HTML></HTML>"). Also AllowNavigation must be set to true.

Interesting bug there me thinks.
Left by Terry Highfield on Aug 11, 2008 11:28 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
In fact I expect you have to wait for the DocumentCompleted event before changing the content on the HTML again.
Left by Terry Highfield on Aug 11, 2008 11:33 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
thanks for the codes

Left by jbr on Sep 03, 2008 3:44 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I just find it interesting that this thread went on for about 3 years. Crappy M$ code :D...
Left by Ghiutzu on Sep 15, 2008 11:12 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
i am showing .doc /.pdf/ .xls with webbrowser.url= ....doc .But i want not to allow user to write or edit in webbrowser open file. question for window control.
Left by karry on Sep 30, 2008 9:44 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Thanks Paul!

I had a similiar issue. It seemed the set property of DocumentText would fail on the first attempt. I have the AllowNavigation flag set to "true". Paul's solution worked like a champ.

I loaded my HTML from a byte array:

this._browserViewCtrl.Navigate("about:blank");
HtmlDocument doc = this._browserViewCtrl.Document;
doc.Write(string.Empty);
this._browserViewCtrl.DocumentText = Encoding.ASCII.GetString(att.Bytes);
Left by Bryant Cobarrubias on Oct 24, 2008 5:17 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
This solution worked for me, but in the Compact Version 2.0 (.NETCF 2.0), maybe it would work in full version too:

just call

WebBrowserCtrl.Stop();

before

WebBrowserCtrl.DocumentText = "whatever";
Left by Hugo lima on Oct 27, 2008 2:40 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Hugo lima

worked like a charm.. as so simple
Left by Jon on Nov 07, 2008 9:37 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
When I use WebBrowserCtrl.Stop(), I get a navigation has been stopped message, not surprisingly.

Worked better for me to use WebBrowserCtrl.Document.Write("somecontent");
Left by John Ayling on Jan 11, 2009 5:09 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Hi,

I am using Report Services web service to export reports to PDF. There is a ASPX page that performs BinaryWrite to the array of bytes that the reporting service web service returns.

My question is how do load the PDF report on to the WEBBROWSER control?

Any help would be much apprieciated.
Left by Francis on Jan 26, 2009 10:44 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Thanks a lot Josh. You saved me ...
Left by Param on Feb 23, 2009 3:21 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Thank you Josh!

This very simple trick worked for me:

if (webBrowser1.Document != null)
webBrowser1.Document.Write(txtDescription.Text);
webBrowser1.DocumentText = txtDescription.Text;
Left by matt on Mar 02, 2009 7:28 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
thanks...
Left by coagantus on Mar 17, 2009 7:39 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I was having a problem with using Navigate to display a new page after the initial one.

Changing the AllowNavigattion property to True allows me to navigate to new pages. I use 3 different ones to inform the user of various information (like a newsletter of sorts).

Thanks for the help all!
Left by Phil on Apr 14, 2009 10:11 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
wb1.Navigate("www.yahoo.com.vn");
while (wb1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
wb1.Navigate("www.google.com.vn");
while (wb1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
Left by trandatnh on May 02, 2009 8:56 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
got it to work for html in a string variable like this...
string htmlDocument = "<html><body> what what </body></html>";

WebBrowser _webBrowser = new WebBrowser();
_webBrowser.AllowNavigation = true;
_webBrowser.DocumentText = htmlDocument;
_webBrowser.Document.Write(htmlDocument);

I fought with it for a while.. and this has worked for me so far..
Left by prospector_x on Jun 17, 2009 7:06 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
webBrowser1.AllowNavigation = false;
allow navigation doesn't matter use wat ever u want .
i face same problem and solved this problem easily see below code.

WebRequest request = WebRequest.Create(url);
request.Method = "GET";
WebResponse response = request.GetResponse();

System.IO.Stream dataStream = response.GetResponseStream();


System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();

webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write(responseFromServer);

reader.Close();
dataStream.Close();
response.Close();

have a nice day .
sameeullah daris
Left by sameeullah Daris on Jul 05, 2009 5:07 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
{
Application.DoEvents();
webbrowser1.DocumentText = bodyStr + "</body></html>";
}

workd for me
Left by fakdap on Sep 28, 2009 3:36 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
my html page body have 2 html textbox.i want add more
inside body.i tried.but already contained texboxes Cleared.after that added.its old body contents and controls cleared.
then add texbox.can u clear me
Left by srinivasan on Jan 02, 2010 6:42 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
I tried many of these tricks and some of them worked one day, but didn't work next day. After long-long debugging I realized that there are mainly 2 issues:
1. WebBrowser control processes many action asynchronously, and it will fail if You ask to him to do something before he finished doing what You asked befor.
while (this.htmlArea.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
did the job for me.
2. If You wan't control to be editable - you should keep AllowNavigation false during edit sessions, but true when You change something programmaticaly.

The last thing I didn't get rid off yet - annoying clicks when changing values ;)
Left by Igor K. on Jan 07, 2010 12:20 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Thanks for the help everyone. I ended up doing the following:

private void ClearBrowser()
{
m_browser.Stop();
if (m_browser.Document != null)
{
m_browser.Document.OpenNew(true);
}
else
{
m_browser.Navigate("about:blank");
while (m_browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
}
Left by usugarbage on Feb 16, 2010 4:28 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
This works great for me!

m_browser.Document.Write("blah blah blah");
Left by Sukesh on Feb 19, 2010 5:35 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
This works great for me!

Left by qzal on May 28, 2010 11:36 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
This worked for me. Thanks to James Booze

Private Sub UpdateHTML(ByVal browser As WebBrowser, ByVal htmlCode As String)
browser.AllowNavigation = True
If browser.Document IsNot Nothing Then
browser.Document.OpenNew(True)
browser.Document.Write(htmlCode)
Else
browser.DocumentText = htmlCode
End If
browser.AllowNavigation = False
End Sub
Left by JM on Nov 17, 2010 2:46 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
Very simple solution, just put/customize this code into sub/function where you want to update/set new HTML text into WebBrowser control:

wbReport.Navigate("about:blank")
wbReport.DocumentText = "your HTML text/string"

and that's all (if needed do Me.Refresh() for the container Form)
Left by Ivan Stojiljkovic on Feb 21, 2011 7:36 PM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
That's normal behaivour.
WebBrowser Control load page asyncronously.
You should for the first time navigate to a
blank page or an url. otherwise its inner objects
do not get instantiate and you get unhandle exception
like "object referense not set to an instance of an
object".

Ramin
Left by طراحی وب سایت on May 13, 2011 7:20 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
It appears it can only be set once; this is a bug in C#. A work around is to run Application.DoEvents();
Left by Dave Rook on Jun 28, 2011 4:14 AM

# re: C# 2.0 WebBrowser control - bug in DocumentText?
Requesting Gravatar...
If you want to write more code on the same window add the code "doc.Refresh" before you doc.Write
Left by knutz on Sep 05, 2011 4:22 PM

Your comment:
 (will show your gravatar)


Copyright © Paul Whitaker | Powered by: GeeksWithBlogs.net | Join free