Internetwork Consulting LLC
3880 Greenhouse Road #421
Houston, TX 77084

832-606-3300

sales@internetworkconsulting.net
Custom Software Development - Computer Repair
 

How to Fix Broken Images, Links, CSS, and Javascript in a C# WebBrowser Control

In writing this I hope to alleviate some potential headaches/roadblocks one may suffer when using a WebBrowser control in C# with html documents that link to external resources (CSS, JS, images).

The problem arises with the method you choose to load the document:

string sHtml = “<html code here>”;

WebBrowser1.DocumentText = sHtml;

In order for the Browser control to load any resources, it must navigate to a document, then it can process any external files required to render it.

WebBrowser1.Navigate(new Uri(docLocation)); //or

WebBrowser1.Document.Url = new Uri(docLocation);

This mean you will have to write the contents to a file before loading the Browser. On another note, don’t write the file to the current working folder or the EXE’s folder as windows has the permissions locked down: it’ll fail in a production environment under “C:\Program Files”. That being said, you can use a %TEMP% folder. But make sure URLs are relative to the temp folder or use absolute URIs.