Home
Manage Your Code
Snippet: Workaround for inability to dynamically set WebBrowser.DocumentText (C#)
Title: Workaround for inability to dynamically set WebBrowser.DocumentText Language: C#
Description: Allows you to dynamically set the HTML for a .NET 2.0 WebBrowser control, bypassing the DocumentText "bug". Views: 387
Author: Andrew Ransom Date Added: 10/25/2007
Copy Code  
1/// <summary>

2/// A workaround to update a WebBrowser control's text

3/// </summary>

4/// <param name="html">The full HTML text to write</param>

5private void WriteDynamicHTMLToWebBrowser(string html)
6{
7    webbrowserControl1.AllowNavigation = false;
8    if (webbrowserControl1.Document != null) 
9        webbrowserControl1.Document.OpenNew(true);
10    else 
11        webbrowserControl1.Navigate("about:blank");
12
13    webbrowserControl1.Document.Write(html);
14}
Usage
WriteDynamicHTMLToWebBrowser("

Hi there!

");
Notes
The .NET 2.0 WebBrowser.DocumentText documentation is quite misleading as it implies you use the DocumentText property to update a WebBrowser's text from a string. In reality, it appears this can only be set once! More discussion is available here: http://geekswithblogs.net/paulwhitblog/archive/2005/12/12/62961.aspx thanks to James Booze's approach in the thread comments for the workaround.