As Thomas W. commented, I almost missed this comment, but I had the same problems, so it’s worth rewriting as the answer I think.
The main problem is that after the first assignment of webBrowser1.DocumentText some html, the subsequent assignments did not affect.
The Thomas-related solution can be found in more detail at http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/displaying-custom-html-in-webbrowser-control.aspx , however I will describe below. if this page becomes unavailable in the future.
In short, because of the way the webBrowser control works, you have to go to a new page every time you want to change the content. Therefore, the author offers a method for updating the control as:
private void DisplayHtml(string html) { webBrowser1.Navigate("about:blank"); if (webBrowser1.Document != null) { webBrowser1.Document.Write(string.Empty); } webBrowser1.DocumentText = html; }
However, I found that in my current application, I get CastException from the if(webBrowser1.Document != null) line if(webBrowser1.Document != null) . I'm not sure why this is the case, but I found that if I complete the entire if block in an attempt to catch the desired effect, it will still work. Cm:
private void DisplayHtml(string html) { webBrowser1.Navigate("about:blank"); try { if (webBrowser1.Document != null) { webBrowser1.Document.Write(string.Empty); } } catch (Exception e) { }
So, every time the DisplayHtml function is executed, I get a CastException from the if , so the contents of the if statement are never reached. However, if I comment on the if so as not to get a CastException, then the browser control is not updated. I suspect there is another side effect of the code underlying the Document property that causes this effect, even though it also throws an exception.
In any case, I hope this helps people.
m3z Dec 03 '13 at 12:20 2013-12-03 12:20
source share