Solved

How can I stop WebBrowser control from ignoring setting DocumentText values?

Posted on 2012-04-04
5
735 Views
Last Modified: 2012-04-10
Hi Guys,

I have a WinForms user control that contains a WebBrowser which is used to edit HTML, and it used to work perfectly.  Lately a number of users have reported that it does not display the HTML assigned to it, and this proves consistently to be the case.  I am not yet certain whether the issue relates to the OS (32 vs 64 bit, XP or Windows 7) or whether it relates to a security update, but I do know that it used to work a treat and now the WebBrowser stays blank.  As far as I am aware nothing has changed in the control code, and this seems to be confirmed by source control.

I have seen many mentions of this issue and suggestions for resolutions on the web and have tried every one I have found, but still nothing!

Here is the basic code that initialises the WebBrowser:
            // Web Browser
            webBrowserBody.DocumentText = string.Empty;
            
            if (webBrowserBody.Document != null)
            {
                webBrowserBody.Document.Click += WebBrowserBodyDocumentClick;
                webBrowserBody.Document.Focusing += WebBrowserBodyDocumentFocusing;
                webBrowserBody.Document.ExecCommand("EditMode", false, null);
                webBrowserBody.Document.ExecCommand("LiveResize", false, null);
            }
            
            webBrowserBody.AllowNavigation = false;
            webBrowserBody.AllowWebBrowserDrop = false;
            webBrowserBody.IsWebBrowserContextMenuEnabled = false;
            webBrowserBody.ScriptErrorsSuppressed = true;

Open in new window


Here is how the bodytext is set:
        public override string Text
        {
            get { return webBrowserBody.DocumentText; }
            set { webBrowserBody.DocumentText = value; }
        }

Open in new window

Here is an example of the HTML retrieved from a database that is being assigned using the getter:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 8.00.6001.18928"></HEAD>
<BODY><FONT size=2 face=Arial>
<P>You received a new telephone message at %%MessageTime%% on 
%%MessageDate%%</P>
<HR>
<BR>
<P><STRONG>Caller: </STRONG>%%Caller%%<BR><STRONG>Company:</STRONG> 
%%CallerCompany%%<STRONG><BR>Telephone: 
</STRONG>%%CallerTelephone%%<BR><STRONG>Email:</STRONG> %%CallerEmail%%</P>
<P><STRONG>Message:</STRONG><BR>%%TelephoneMessage%%</P>
<P>
<HR>

<P></P>
<P>%%User%%<BR>%%CompanyName%%<BR>%%CompanyMailingAddress%%<BR>%%CompanyWebInfo%%</FONT><BR></P></BODY></HTML>

Open in new window

We know that the HTML is valid and works, because we can assign it to an Outlook email and it works perfectly...

Any suggestions as to how I might get this working again?
0
Comment
Question by:chrisbray
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37811583
I have Windows 7 Enterprise 32-bit, and the WebBrowser control won't display HTML, unless I comment out this line:


webBrowserBody.Document.ExecCommand("EditMode", false, null);

Open in new window

0
 
LVL 3

Author Comment

by:chrisbray
ID: 37811701
Hi Bob,

Hmm...  that made no difference on my 64 bit machine, and so far appears to work on 32 bit XP.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37811801
Also, I have IE 9 installed...9.0.8112.16421
0
 
LVL 3

Accepted Solution

by:
chrisbray earned 0 total points
ID: 37814538
Hi Bob,

OK, found a workaround:

        private void SetHtml(string value)
        {
            if (webBrowserBody.Document != null)
            {
                webBrowserBody.Document.OpenNew(true);
            }
            else
            {
                webBrowserBody.Navigate("about:blank");
            }

            if (webBrowserBody.Document != null)
            {
                webBrowserBody.Document.Write(value);
            }
        }

Open in new window


This relies on the following line in the webBrowser initialisation:

webBrowserBody.AllowNavigation = false;

Open in new window


If you wanted you could include it in the setting method.

This code was proposed by Andrew Ransom back in 2007 at http://www.codekeep.net/snippets/1bb8c968-3f30-46ee-96e6-006161a0c04b.aspx indicating that something else has changed in our code if the problem was extant in .Net even then - I know that our dialogs have all worked after that because they were barely thought of at that time....
0
 
LVL 3

Author Closing Comment

by:chrisbray
ID: 37826820
The answer was found based on a snippet posted by a third party.

Thanks as always to TheLearnedOne who always makes every effort to help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.Security 2 29
Google Directions API to Map URL -C#? 3 39
length of the password hash sha1:64000 to set sql field property. 13 66
Problem to Office 1 18
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question