42

In my vb.net program, I am using a webbrowser to show the user an HTML preview. I was previously hitting a server to grab the HTML, then returning on an asynchronous thread and raising an event to populate the WebBrowser.DocumentText with the HTML string I was returning.

Now I set it up to grab all of the information on the client, without ever having to hit the server, and I'm trying to raise the same event. I watch the code go through, and it has the HTML string correct and everything, but when I try to do

browser.DocumentText = _emailHTML

the contents of DocumentText remain as "<HTML></HTML>"

I was just wondering why the DocumentText was not being set. Anyone have any suggestions?

johnc
  • 39,385
  • 37
  • 101
  • 139
Joe Morgan
  • 1,761
  • 6
  • 20
  • 29

9 Answers9

59

Try the following:

browser.Navigate("about:blank");
HtmlDocument doc = browser.Document;
doc.Write(String.Empty);
browser.DocumentText = _emailHTML;

I've found that the WebBrowser control usually needs to be initialized to about:blank anyway. The same needs to be done between navigates to different types of content (like text/xml to text/html) because the renderer is different (mshtml for text/html, something else for text/xml).

See Also: C# 2.0 WebBrowser control - bug in DocumentText?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
David Mohundro
  • 11,922
  • 5
  • 40
  • 44
  • 5
    I appreciate the help. Turns out I was able to get it with browser.Document.OpenNew(True) You're right - I needed to initialize it. – Joe Morgan Oct 06 '08 at 14:32
  • 2
    Why the doc.Write followed by DocumentText = ...? Isn't it enough just to do a browser.Document.Write(_emailHTML);? – Nikodemus RIP Mar 28 '12 at 14:03
  • 1
    Don't think this method works if `AllowNavigation` is set to false. – Brad Moore Jan 30 '13 at 03:14
  • 1
    Thanks, I was struggling with this issue for hours... Yet, I do not understand why the solution works. Could explain why doc.Write(...) needed? – Moonwalker Feb 06 '13 at 16:53
  • It's been a few years since I worked with the browser control, but if I remember correctly, it was related to the initialization... it certainly isn't ideal, but neither is having to navigate to about:blank as part of initialization either. I'd recommend creating a method called "InitializeWebBrowser" or something so that the intent is clear. – David Mohundro Feb 06 '13 at 17:27
38

I found the following and it worked!

    webBrowser.Navigate("about:blank");
    webBrowser.Document.OpenNew(false);
    webBrowser.Document.Write(html);
    webBrowser.Refresh();
Matthias
  • 481
  • 4
  • 3
11

I found the best way to handle this, is as follows:

if (this.webBrowser1.Document == null)
{
    this.webBrowser1.DocumentText = htmlSource;
}
else
{
    this.webBrowser1.Document.OpenNew(true);
    this.webBrowser1.Document.Write(htmlSource);
}
FreddieH
  • 773
  • 8
  • 18
3

That worked for me:

webBrowser.Navigate("about:blank");
webBrowser.Document?.Write(htmlString);
Interferank
  • 101
  • 6
2

Make sure that you do not cancel Navigating event of WebBrowser for about:blank page. WebBrowser navigates to about:blank before setting DocumentText. So if you want to handle links by yourself you need to create following handler of Navigating event:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    if(e.Url.OriginalString.StartsWith("about:"))
    {
        return;
    }
    e.Cancel = true;
    // ...
}
antgraf
  • 155
  • 2
  • 7
1

Just spotted this in some of our old code.

_webBrowser.DocumentText = builder.WriteToString( ... );

Application.DoEvents();

Apparently a DoEvents also kicks the browser into rendering

johnc
  • 39,385
  • 37
  • 101
  • 139
  • 1
    It isn't surprising that this might work, but I'd recommend avoiding using DoEvents when possible. It tells the message loop to stop everything and process all messages in the queue. See http://www.codinghorror.com/blog/2004/12/is-doevents-evil.html. – David Mohundro Feb 06 '13 at 17:29
0

please refer to this answer c# filenotfoundexception on webbrowser?

Community
  • 1
  • 1
JOE SKEET
  • 7,950
  • 14
  • 48
  • 64
0

While Application.DoEvents() fix it in a WinForms project, it was irrelevant in a WPF project.

I finally got it to work by using webBrowser.Write( htmlContent ) (instead of webBrowser.DocumentText = htmlContent).

AVIDeveloper
  • 2,954
  • 1
  • 25
  • 32
0

This always works

using mshtml;


private IHTMLDocument2 Document
{
    get
    {
        if (Browser.Document != null)
        {
            return Browser.Document.DomDocument as IHTMLDocument2;
        }

        return null;
    }
}


if (Document == null)
{
    Browser.DocumentText = Contents;
}
else
{
    Document.body.innerHTML = Contents;
}
Prads
  • 71
  • 2
  • 4