How to display the string html contents into webbrowser control?

前端 未结 9 1507
不知归路
不知归路 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:29

    Instead of navigating to blank, you can do

    webBrowser1.DocumentText="0";
    webBrowser1.Document.OpenNew(true);
    webBrowser1.Document.Write(theHTML);
    webBrowser1.Refresh();
    

    No need to wait for events or anything else. You can check the MSDN for OpenNew, while I have tested the initial DocumentText assignment in one of my projects and it works.

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

    Here is a little code. It works (for me) at any subsequent html code change of the WebBrowser control. You may adapt it to your specific needs.

        static public void SetWebBrowserHtml(WebBrowser Browser, string HtmlText)
        {
            if (Browser != null)
            {
                if (string.IsNullOrWhiteSpace(HtmlText))
                {
                    // Putting a div inside body forces control to use div instead of P (paragraph)
                    // when the user presses the enter button
                    HtmlText = 
                            @"<html>
                        <head>
                        <meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />
                        </head>
                          <div></div>
                        <body>
                        </body>
                        </html>";
                }
    
                if (Browser.Document == null)
                {
                    Browser.Navigate("about:blank");
    
                    //Wait for document to finish loading
                    while (Browser.ReadyState != WebBrowserReadyState.Complete)
                    {
                        Application.DoEvents();
                        System.Threading.Thread.Sleep(5);
                    }
                }
    
                // Write html code
                dynamic Doc = Browser.Document.DomDocument;
                Doc.open();
                Doc.write(HtmlText);
                Doc.close();
    
    
                // Add scripts here 
                /*  
                dynamic Doc = Document.DomDocument;
                dynamic Script = Doc.getElementById("MyScriptFunctions");
                if (Script == null)
                {
                    Script = Doc.createElement("script");
                    Script.id = "MyScriptFunctions";
                    Script.text = JavascriptFunctionsSourcecode;
                    Doc.appendChild(Script);
                }                 
                */
    
    
    
                // Enable contentEditable   
                /*  
                if (Browser.Document.Body != null)
                {
                    if (Browser.Version.Major >= 9)
                        Browser.Document.Body.SetAttribute("contentEditable", "true");
                }             
                 */
    
                // Attach event handlers
                // Browser.Document.AttachEventHandler("onkeyup", BrowserKeyUp);
                // Browser.Document.AttachEventHandler("onkeypress", BrowserKeyPress);
                // etc...
            }
        }        
    
    0 讨论(0)
  • 2020-11-27 07:35

    The DisplayHtml(string html) recommended by m3z worked for me.

    In case it helps somebody, I would also like to mention that initially there were some spaces in my HTML that invalidated the HTML and so the text appeared as a string. The spaces were introduced (around the angular brackets) when I pasted the HTML into Visual Studio. So if your text is still appearing as text after you try the solutions mentioned in this post, then it may be worth checking that the HTML syntax is correct.

    0 讨论(0)
提交回复
热议问题