Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-04-04
5
Medium Priority
?
756 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Loops Section Overview
Suggested Courses

610 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