Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

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 property to 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.

C#

private void DisplayHtml(string html)

{

    webBrowser1.Navigate("about:blank");

    if (webBrowser1.Document != null)

    {

        webBrowser1.Document.Write(string.Empty);

    }

    webBrowser1.DocumentText = html;

}


VB.NET

Private Sub DisplayHtml(ByVal html As String)

    webBrowser1.Navigate("about:blank")

    If webBrowser1.Document IsNot Nothing Then

        webBrowser1.Document.Write(String.Empty)

    End If

    webBrowser1.DocumentText = html

End Sub


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 is Navigating event of WebBrowser. In Navigating event we can do our filtering.

C#

private void webBrowser1_Navigating(object sender,

        WebBrowserNavigatingEventArgs e)

{

    if (e.Url.ToString() != "about:blank")

        e.Cancel = true;

}


VB.NET

Private Sub webBrowser1_Navigating(ByVal sender As Object, _

            ByVal e As WebBrowserNavigatingEventArgs)

    If e.Url.ToString() <> "about:blank" Then

        e.Cancel = True

    End If

End Sub


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!


kick it on DotNetKicks.com pimp it Progg it 顶 Shout it

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# August 15, 2009 3:55 AM

DotNetBurner - Windows Forms said:

DotNetBurner - burning hot .net content

# August 15, 2009 3:56 AM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# August 15, 2009 3:58 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# August 15, 2009 3:58 AM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# August 15, 2009 3:59 AM

9eFish said:

9efish.感谢你的文章 - Trackback from 9eFish

# August 15, 2009 4:01 AM

Mohamed Meligy said:

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

# August 15, 2009 5:04 AM

DigiMortal said:

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

# August 15, 2009 5:23 AM

Displaying custom HTML in WebBrowser control – Gunnar Peipman's … | Webmaster Tools said:

Pingback from  Displaying custom HTML in WebBrowser control &#8211; Gunnar Peipman&#039;s &#8230; | Webmaster Tools

# August 15, 2009 10:50 AM

Steven said:

ThankYOU, that realy solved all my problems!

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

# August 30, 2009 1:29 PM

Automated Web Testing with the WebBrowserControl said:

Pingback from  Automated Web Testing with the WebBrowserControl

# September 11, 2009 8:12 AM

Max Gunther said:

Thank YOU, that realy solved all my problems !

# February 17, 2010 12:00 PM

ThePrivateGeek said:

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

# May 16, 2011 9:14 AM

display a xml within a web browser control without XSL - Programmers Goodies said:

Pingback from  display a xml within a web browser control without XSL - Programmers Goodies

# September 6, 2011 8:00 AM

Alex said:

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

# November 10, 2011 6:39 AM

alex said:

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]

# November 15, 2011 4:18 PM

C# by stevekr - Pearltrees said:

Pingback from  C# by stevekr - Pearltrees

# January 8, 2012 7:24 AM

Water Damage Cleanup said:

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

# January 20, 2012 10:49 PM

Ankit Singh said:

Hi,

I was reading your article and I would like to appreciate you for making it very simple and understandable. This article gives me a basic idea of web browser control in c#.Net and it helped me a lot. I have found another nice post over the internet which also explained very well about web browser control in C#.Net, please check this url for that article...

www.mindstick.com/.../bd89779f-66e6-4497-94ee-de2821473e7a

Thank you very much!

# February 25, 2012 3:38 AM