Solved

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

Posted on 2012-04-04
5
728 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now