C# 2.0 WebBrowser control - bug in DocumentText?

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;

Print | posted @ Monday, December 12, 2005 10:30 PM

Comments on this entry:

Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Szymon at 12/13/2005 9:54 AM

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?
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Szymon at 12/13/2005 9:57 AM

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?
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by David at 12/22/2005 2:31 PM

I am having the same problem you had with sequential DocumentText assignments in a WebBrowser control. Have you run into any solutions or workarounds?
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Paul Whitaker at 12/22/2005 2:32 PM

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.
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Sally at 1/11/2006 8:14 AM

Set AllowNavigation to true, then you can assign to DocumentText as much as you want.
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Ishai at 2/14/2006 3:01 AM

doesnt help me in my VSTO application
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by archer at 2/16/2006 7:13 AM

I found the solution here: http://www.breakoutseason.com/derrick%5Fribilla/webbrowser_control_setting_dynamically_not_working_solution_cant_set.htm
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Dylan at 3/7/2006 8:13 AM

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?
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Mike Westemeir at 4/27/2006 11:02 AM

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);

}
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Terri Policy at 5/5/2006 6:02 PM

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
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Seba at 6/12/2006 9:21 AM

Have anyone solve problem with DocumentText ?
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Paul Whitaker at 6/12/2006 11:07 AM

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.
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Rob Cecil at 6/12/2006 10:56 PM

I've tried all the above techniques without success, including the control replacement. Why is this such a bugger?!

Any progress by anyone?
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Rob Cecil at 6/13/2006 1:09 AM

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!
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by m,pe,za at 9/24/2006 6:19 PM

this.webBrowser1.Document.OpenNew(true);
...
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by James Booze at 10/10/2006 4:19 PM

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";
}
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Maverick at 10/20/2006 9:53 PM

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?
Gravatar # Winforms: web browser control trouble
by TryParse(this) at 1/7/2007 2:49 PM

The last couple of days I had a big fight with the web browser control that ships with Visual Studio
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Mesut Sahin Boydas at 2/16/2007 6:59 PM

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);
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by AngeDeLaMort at 2/23/2007 3:28 AM

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.
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by James Chai at 2/23/2007 7:31 AM

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.
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Andrew at 2/27/2007 10:17 PM

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");
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Rob Heeley at 3/1/2007 12:47 PM

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.
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Galib Anwar at 7/2/2007 1:03 PM

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;
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by walty at 7/2/2007 11:46 PM

thanks Booze, your trick of

Document.OpenNew(true)

works for me

Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by = at 10/17/2007 1:42 AM


..
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Martin Kissel at 10/22/2007 12:11 AM

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
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Josh Comley at 11/8/2007 10:28 PM

This very simple trick worked for me:

if (webBrowser1.Document != null)
webBrowser1.Document.Write(txtDescription.Text);
webBrowser1.DocumentText = txtDescription.Text;
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Michael T at 12/3/2007 11:05 PM

I had experienced all of this as well, but my solution was much simpler:

webBrowser.Document.Clear();
webBrowser.Document.Write( myHTML );
webBrowser.Document.Close();
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by hans at 2/27/2008 11:07 PM

Use the Navigated event!
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by gka at 3/19/2008 4:43 AM

read MSDN CAREFULLY!
Once you assign the DocumentText you trigger a Navigation. After the DocumentCompleted then you can do another assignment!
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Mat at 5/10/2008 7:39 AM

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>";
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Max at 5/20/2008 6:22 PM

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!
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by a at 5/21/2008 3:06 AM







Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Terry Highfield at 8/11/2008 11:28 PM

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.
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Terry Highfield at 8/11/2008 11:33 PM

In fact I expect you have to wait for the DocumentCompleted event before changing the content on the HTML again.
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by jbr at 9/3/2008 3:44 PM

thanks for the codes

Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by Ghiutzu at 9/15/2008 11:12 PM

I just find it interesting that this thread went on for about 3 years. Crappy M$ code :D...
Gravatar # re: C# 2.0 WebBrowser control - bug in DocumentText?
by karry at 9/30/2008 9:44 PM

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.

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 1 and 8 and type the answer here: