?
Solved

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

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

770 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