.Net WebBrowser.DocumentText Isn't Changing!

后端 未结 9 2098
不思量自难忘°
不思量自难忘° 2020-12-09 01:14

In my vb.net program, I am using a webbrowser to show the user an HTML preview. I was previously hitting a server to grab the HTML, then returning on an asynchronous thread

相关标签:
9条回答
  • 2020-12-09 01:55

    Try the following:

    browser.Navigate("about:blank");
    HtmlDocument doc = browser.Document;
    doc.Write(String.Empty);
    browser.DocumentText = _emailHTML;
    

    I've found that the WebBrowser control usually needs to be initialized to about:blank anyway. The same needs to be done between navigates to different types of content (like text/xml to text/html) because the renderer is different (mshtml for text/html, something else for text/xml).

    (via http://geekswithblogs.net/paulwhitblog/archive/2005/12/12/62961.aspx)

    0 讨论(0)
  • 2020-12-09 01:57

    I found the best way to handle this, is as follows:

    if (this.webBrowser1.Document == null)
    {
        this.webBrowser1.DocumentText = htmlSource;
    }
    else
    {
        this.webBrowser1.Document.OpenNew(true);
        this.webBrowser1.Document.Write(htmlSource);
    }
    
    0 讨论(0)
  • 2020-12-09 01:58

    Make sure that you do not cancel Navigating event of WebBrowser for about:blank page. WebBrowser navigates to about:blank before setting DocumentText. So if you want to handle links by yourself you need to create following handler of Navigating event:

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        if(e.Url.OriginalString.StartsWith("about:"))
        {
            return;
        }
        e.Cancel = true;
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题