Solved

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

Posted on 2012-04-04
5
739 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

734 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