How to display the string html contents into webbrowser control?

前端 未结 9 1506
不知归路
不知归路 2020-11-27 06:47

I have a c# win app program. I save the text with html format in my database but I want to show it in a webbrowser to my user.How to display the string html contents into we

相关标签:
9条回答
  • 2020-11-27 07:09

    webBrowser.NavigateToString(yourString);

    0 讨论(0)
  • 2020-11-27 07:12

    As commented by Thomas W. - I almost missed this comment but I had the same issues so it's worth rewriting as an answer I think.

    The main issue being that after the first assignment of webBrowser1.DocumentText to some html, subsequent assignments had no effect.

    The solution as linked by Thomas can be found in detail at http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/displaying-custom-html-in-webbrowser-control.aspx however I will summarize below in case this page becomes unavailable in the future.

    In short, due to the way the webBrowser control works, you must navigate to a new page each time you wish to change the content. Therefore the author proposes a method to update the control as:

    private void DisplayHtml(string html)
    {
        webBrowser1.Navigate("about:blank");
        if (webBrowser1.Document != null)
        {
            webBrowser1.Document.Write(string.Empty);
        }
        webBrowser1.DocumentText = html;
    }
    

    I have however found that in my current application I get a CastException from the line if(webBrowser1.Document != null). I'm not sure why this is, but I've found that if I wrap the whole if block in a try catch the desired effect still works. See:

    private void DisplayHtml(string html)
    {
        webBrowser1.Navigate("about:blank");
        try
        {
            if (webBrowser1.Document != null)
            {
                webBrowser1.Document.Write(string.Empty);
            }
        }
        catch (CastException e)
        { } // do nothing with this
        webBrowser1.DocumentText = html;
    }
    

    So every time the function to DisplayHtml is executed I receive a CastException from the if statement, so the contents of the if statement are never reached. However if I comment out the if statement so as not to receive the CastException, then the browser control doesn't get updated. I suspect there is another side effect of the code behind the Document property which causes this effect despite the fact that it also throws an exception.

    Anyway I hope this helps people.

    0 讨论(0)
  • 2020-11-27 07:17

    Old question, but here's my go-to for this operation.

    If browser.Document IsNot Nothing Then
        browser.Document.OpenNew(True)
        browser.Document.Write(My.Resources.htmlTemplate)
    Else
        browser.DocumentText = My.Resources.htmlTemplate
    End If
    

    And be sure that any browser.Navigating event DOES NOT cancel "about:blank" URLs. Example event below for full control of WebBrowser navigating.

    Private Sub browser_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles browser.Navigating
    
        Try
            Me.Cursor = Cursors.WaitCursor
    
            Select Case e.Url.Scheme
    
                Case Constants.App_Url_Scheme
    
                    Dim query As Specialized.NameValueCollection = System.Web.HttpUtility.ParseQueryString(e.Url.Query)
    
                    Select Case e.Url.Host
    
                        Case Constants.Navigation.URLs.ToggleExpander.Host
    
                            Dim nodeID As String = query.Item(Constants.Navigation.URLs.ToggleExpander.Parameters.NodeID)
    
                            :
                            :
                            <other operations here>
                            :
                            :
    
                    End Select
    
                Case Else
                    e.Cancel = (e.Url.ToString() <> "about:blank")
    
            End Select
    
        Catch ex As Exception
            ExceptionBox.Show(ex, "Operation failed.")
        Finally
            Me.Cursor = Cursors.Default
        End Try
    
    End Sub
    
    0 讨论(0)
  • 2020-11-27 07:18

    For some reason the code supplied by m3z (with the DisplayHtml(string) method) is not working in my case (except first time). I'm always displaying html from string. Here is my version after the battle with the WebBrowser control:

    webBrowser1.Navigate("about:blank");
    while (webBrowser1.Document == null || webBrowser1.Document.Body == null)
        Application.DoEvents();
    webBrowser1.Document.OpenNew(true).Write(html);
    

    Working every time for me. I hope it helps someone.

    0 讨论(0)
  • 2020-11-27 07:21

    Try this:

    webBrowser1.DocumentText =
        "<html><body>Please enter your name:<br/>" +
        "<input type='text' name='userName'/><br/>" +
        "<a href='http://www.microsoft.com'>continue</a>" +
        "</body></html>";
    
    0 讨论(0)
  • 2020-11-27 07:25

    Simple solution, I've tested is

    webBrowser1.Refresh();
    var str = "<html><head></head><body>" + sender.ToString() + "</body></html>";
    webBrowser1.DocumentText = str;
    
    0 讨论(0)
提交回复
热议问题