Embedded WebBrowser in Windows Form C# project
I have a form with an embedded web browser control on开发者_运维问答 it. I am currently using WebBrowser
and use it like so:
webBrowser1.Navigate("about:blank");
HtmlDocument doc = this.webBrowser1.Document;
doc.Write(string.Empty);
String htmlContent = GetHTML();
doc.Write(htmlContent);
This writes the HTML correctly to the web browser control BUT it never clears the existing data and it just appends, so I end up with N web pages stacked on top of each other.
Is this the best control to use? If so why is it not clearing existing data?
You need to use:
HtmlDocument doc = this.webBrowser1.Document.OpenNew(true);
now the contents of the document will be cleared before writing.
All calls to Write should be preceded by a call to OpenNew, which will clear the current document and all of its variables. Your calls to Write will create a new HTML document in its place. To change only a specific portion of the document, obtain the appropriate HtmlElement and set its InnerHtml property.
Yes, it is.
You should be able to call the Clear method if you need to clear contents.
Check this article for in-depth details and sample code:
http://www.codeproject.com/KB/miscctrl/simplebrowserformfc.aspx
Call HtmlDocument.OpenNew between pages:
OpenNew will clear the previous loaded document, including any associated state, such as variables. It will not cause navigation events in WebBrowser to be raised.
精彩评论