0

I want to have a webBrowser open a HTML string. This is what I got so far for opening the HTML string in a webBrowser:

public void show()
{
    routelistBrowser.ScriptErrorsSuppressed = true;
    reisOpties = fakeLijst(gekozenTijd);

    RouteListPanel rlp = new RouteListPanel();
    rlp.Session = new Dictionary<string, object>();
    rlp.Session["reisOpties"] = reisOpties;
    rlp.Initialize();
    Console.WriteLine(rlp.TransformText());
    DisplayHtml(rlp.TransformText());
}

private void DisplayHtml(string html)
    {
        routelistBrowser.Navigate(""); 
        HtmlDocument doc = routelistBrowser.Document.OpenNew(true); 
        doc.Write(html);
        routelistBrowser.Refresh();
        Console.WriteLine(html);
    }

My problem is that my webBrowser won't load this HTML string. I am not sure what I am doing wrong. I have added the webBrowser in a User Control and added that User Control in the Main Form. The webBrowser will show, but will not load the HTML string. Output Console (HTML string):

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=11" />
    <title></title>
    <style type="text/css">
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id ="container" class="container">
        <ul style="list-style-type:none;">
         <li>
             <div>
                <h1>17:54 --> 18:13</h1><h3>Totale tijd: 0:19 
                Aantaloverstappen: 0x Perron: 1a</h3>
                <h3>NS - Intercity</h3>
             </div>
         </li>
        </ul> 
        <ul style="list-style-type:none;">
         <li>
             <div>
                <h1>18:00 --> 18:19</h1><h3>Totale tijd: 0:19 
                Aantaloverstappen: 0x Perron: 1a</h3>
                <h3>NS - Intercity</h3>
             </div>
         </li>
        </ul> 
        <ul style="list-style-type:none;">
         <li>
             <div>
                <h1>18:06 --> 18:25</h1><h3>Totale tijd: 0:19 
                Aantaloverstappen: 0x Perron: 1a</h3>
                <h3>NS - Intercity</h3>
             </div>
         </li>
        </ul> 
        <ul style="list-style-type:none;">
         <li>
             <div>
                <h1>18:12 --> 18:31</h1><h3>Totale tijd: 0:19 
                Aantaloverstappen: 0x Perron: 1a</h3>
                <h3>NS - Intercity</h3>
             </div>
         </li>
        </ul> 
        <ul style="list-style-type:none;">
         <li>
             <div>
                <h1>18:18 --> 18:37</h1><h3>Totale tijd: 0:19 
                Aantaloverstappen: 0x Perron: 1a</h3>
                <h3>NS - Intercity</h3>
             </div>
         </li>
        </ul> 
        <ul style="list-style-type:none;">
         <li>
             <div>
                <h1>18:24 --> 18:43</h1><h3>Totale tijd: 0:19 
                Aantaloverstappen: 0x Perron: 1a</h3>
                <h3>NS - Intercity</h3>
             </div>
         </li>
        </ul> 
        <ul style="list-style-type:none;">
         <li>
             <div>
                <h1>18:30 --> 18:49</h1><h3>Totale tijd: 0:19 
                Aantaloverstappen: 0x Perron: 1a</h3>
                <h3>NS - Intercity</h3>
             </div>
         </li>
        </ul> 
    </div>
</body>
</html>
Danjhi
  • 1
  • 3
  • Why do you need to navigate to `about:blank` and write empty string to document? – Pavel Anikhouski Dec 08 '19 at 16:35
  • Have you tried putting a breakpoint in the `DisplayHtml()` method to see if the value of `html` is indeed correctly set? There is also an exception handler in that method that does nothing, so if an exception is being thrown, you wouldn't otherwise know about it. Right? – robbpriestley Dec 08 '19 at 16:36
  • @robbpriestley I have tried putting a breakpoint and the value is `html` correctly set. I have added a `Console.WriteLine(e);` for the exception but there is no exception thrown. Do you know if it is maybe some webBrowser setting I need to change or something like that? – Danjhi Dec 08 '19 at 16:48
  • `routelistBrowser.Navigate(""); HtmlDocument doc = routelistBrowser.Document.OpenNew(true); doc.Write("Your Html String or file content"); routelistBrowser.Refresh();` – Jimi Dec 08 '19 at 17:04
  • @Jimi Thanks for your sugguestion, but it still doesn't seem to load the page correctly. – Danjhi Dec 08 '19 at 17:07
  • What does *correctly* mean? What is not *right* about it? Btw, change the Browser compatibility `` description in ``. Also, if you haven't activated the Browser extended features, do as described here: [How can I get the WebBrowser control to show modern contents?](https://stackoverflow.com/a/38514446/7444103) – Jimi Dec 08 '19 at 17:33
  • What type is `routelistBrowser`, exactly? – robbpriestley Dec 08 '19 at 17:44
  • @robbpriestley what do you mean with "what type is routelistBrowser"? routelistBrowser is a webBrowser if that is what you want to know. – Danjhi Dec 08 '19 at 19:38
  • Does this answer your question? [.Net WebBrowser.DocumentText Isn't Changing!](https://stackoverflow.com/questions/174403/net-webbrowser-documenttext-isnt-changing) – robbpriestley Dec 08 '19 at 19:44
  • @Jimi I mean with "correctly" that the webBrowser won't show the HTML string at all, it is just blank. The webBrowser works when I put a url at the beginning (for tesing porposes only), but when I try to load the HTML string (or any other url for testing purposes only), it won't change/load the new page in the webBrowser. – Danjhi Dec 08 '19 at 19:45
  • @robbpriestley Thanks for your suggestion, I have already tried this and it still won't work. I got this output in my console when I try to load the HTML String: `'WindowsFormsApp4.exe' (CLR v4.0.30319: WindowsFormsApp4.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.resources\v4.0_4.0.0.0_nl_b77a5c561934e089\System.resources.dll'. Module was built without symbols. Exception thrown: 'System.UriFormatException' in System.dll` I am not sure if anything of this has to do with my error. – Danjhi Dec 08 '19 at 19:49
  • Have you used the code I posted exactly how it is? `Refresh()` included? And `.Navigate("")` instead of `.Navigate("about:blank")`. This is how it has always been, there's no reason it shouldn't work for you. See that you're using the correct browser reference, just in case... – Jimi Dec 08 '19 at 20:09
  • @Jimi I have used your code that you posted. It just doesn't seem to work. The whole HTML string is correct. I have used the webBrowser in a User Control and added this User Control in my main form. After a button click the webBrowser needs to load the HTML string. I am not sure if that makes any difference. – Danjhi Dec 08 '19 at 21:23
  • You have a `routelistBrowser` object and a `webBrowserRoute` object. Is this real code? Or you actually have 2 different WebBrowser objects? Make a simple test: create a new Project with just one Form and one WebBrowser control and write to the HtmlDocument the html you have posted here, exactly how it is. – Jimi Dec 08 '19 at 22:00
  • @Jimi I have changed the webBrowser name in my project and forgot to change all of the names in this question. I have done what you said, I have put all the same code in the form1_Load methode and it finally loads. But I am still not sure why it won't load in my main project method `Show`. Does it have something to do with that I call the method `Show` in a other `UserControl`instead of a `Form`, or that I don't call show in the `Form_Load method`? – Danjhi Dec 09 '19 at 10:37
  • It might be the case. How this UserControl knows about a WebBrowser control it doesn't own? Are you passing a reference to it, at some point? How? It's very important, in a question, to provide the full context in which the code presented operates. Possibly, add these informations to your question. As you could test, the WebBrowser can show that html page, thus it's something else. Maybe you're not actually using the correct WebBrowser reference (or you're new-ing it). – Jimi Dec 09 '19 at 11:21

0 Answers0