How to display html string content in webbrowser control?

I have a program to run C # win. I save the text in html format in my database, but I want to display it in a web browser to my user. How to display html string content in webbrowser control?

early

+48
c # browser
Mar 19 2018-11-11T00:
source share
8 answers

Try the following:

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>"; 
+94
Mar 19 '11 at 2:33 p.m.
source share

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) { } // do nothing with this webBrowser1.DocumentText = html; } 

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.

+21
Dec 03 '13 at 12:20
source share

Instead of going to space, 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 tested the initial purpose of DocumentText in one of my projects and it works.

+20
May 19 '14 at 10:59
source share

For some reason, the code provided by m3z (using the DisplayHtml(string) method) does not work in my case (except for the first time). I always show html from a 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); 

I work every time for me. Hope this helps someone.

+6
Mar 14 '14 at 14:11
source share

The simple solution I tested is

 webBrowser1.Refresh(); var str = "<html><head></head><body>" + sender.ToString() + "</body></html>"; webBrowser1.DocumentText = str; 
+4
Apr 23 '16 at 12:24
source share

webBrowser.NavigateToString (yourString);

+3
Feb 28 '13 at 7:14
source share

Here is a little code. It works (for me) with any subsequent change to the HTML code of the WebBrowser control. You can 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... } } 
+2
Apr 14 '15 at 18:14
source share

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

In case this helps someone, I would also like to mention that initially in my HTML there were some spaces that were not valid for HTML, and therefore the text appeared as a string. Spaces were introduced (around the angular brackets) when I pasted HTML in Visual Studio. Therefore, if your text is still displayed as text after you try to find the solutions mentioned in this post, it might be worth checking the HTML syntax is correct.

0
Apr 20 '17 at 15:05
source share



All Articles