Displaying custom HTML in WebBrowser control

I am using WebBrowser control to show preview of automatically generated HTML. Users can select options and preview pane reflects those changes automatically. WebBrowser control has some problems that have been here for years already. Here is my example about how to show custom HTML in WebBrowser control.

Problem

When we look at methods and properties of WebBrowser control our common sense tells us that something like this should work (VB.NET users: delete “;” after this line):

webBrowser1.DocumentText = "some text here";

And it works – only once. All subsequent assignments change nothing. When we try to refresh WebBrowser control we get only white are. Setting AllowNavigation propertyto true – some guys suggest it – has no effect also.

Solution

To get all assignments works after first one we need to navigate to some page. about:blank is good candidate because it “exists” also in local machine for sure. And that’s not enough – we need to write something to document. After that we can show our custom HTML.

private void DisplayHtml(string html)
{
    webBrowser1.Navigate(
"about:blank"
);
   
if (webBrowser1.Document != null
)
    {
        webBrowser1.Document.Write(
string.Empty);
    }
    webBrowser1.DocumentText = html;
}

NB! You should set AllowNavigation property to true before you deal with contents shown to users.

Keeping users on generated page

Now we can show HTML correctly and everything seems to be okay. But are we finished? Answer is yes if we want users to be able to follow the links we show to them and no if we want to keep users on generated page.

Currently we allowed navigation because otherwise we cannot move to about:blank. We have to allow this navigation and disable all other navigation. Fortunately there isNavigating event of WebBrowser. In Navigating event we can do our filtering.

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
   
if (e.Url.ToString() != "about:blank"
)
        e.Cancel =
true;
}

So, that’s it. We can now display our custom HTML in WebBrowser control as many times as we want. If you have some better trick then please let me know!

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    8 thoughts on “Displaying custom HTML in WebBrowser control

    • August 15, 2009 at 9:04 am
      Permalink

      hmm .. what would happen if you enable navigation, navigate to blank, and then disable navigation again?

    • August 15, 2009 at 9:23 am
      Permalink

      Well, it was one of the first thing I tried. But it doesn’t work.

    • August 30, 2009 at 5:29 pm
      Permalink

      ThankYOU, that realy solved all my problems!

      Making a litle twitter c# app so custom html is needed, Thanks!

    • February 17, 2010 at 5:00 pm
      Permalink

      Thank YOU, that realy solved all my problems !

    • May 16, 2011 at 1:14 pm
      Permalink

      What if the design mode was enabled for the browser control? If you have design mode enabled the allowNavigate should be false for the design mode to work. I am building a web site builder which is based on browser control (in design mode) and mshtml.dll.
      Any tips would be highly appreciated. Thanks

    • November 10, 2011 at 11:39 am
      Permalink

      Thanks, this solved the problem that using only DocumentText property inside a Vb application it works only the first time.

    • November 15, 2011 at 9:18 pm
      Permalink

      How can i make this work for vba? I”m using this in ms access. This below works but when i move to another record, it does not update the activex webbrowser. It goes blank.

      Forms![frm_EmailViewerTest].Form.WebBrowser2.Navigate “about:blank”

      Forms![frm_EmailViewerTest].Form.WebBrowser2.Document.Write Forms![frm_EmailViewerTest].[TestEmails_subform].[Form]![EMLText]

    • January 21, 2012 at 3:49 am
      Permalink

      Great article! I am trying to run mozilla in my webbrowser control, having some issues…

    Leave a Reply

    Your email address will not be published. Required fields are marked *