WebBrowser control and Stylesheet
Hi
apologies if I'm doing something stupid here, but....
I have a Windows Form that has a WebBrowser control on it. I'm applying an Xslt transfrom to some Xml and setting the DocumentText of the WebBrowser to my my generated Html. The Html refers to a stylesheet which is local to the application's directory...i.e. bin/debug. However, the styles are not being applied. The styles ARE applied if the link to the stylesheet within the Html includes the complete path to stylesheet, but I really want to avoid hard-coded paths within the Html.
Can anyone shed any light?
Thanks
[586 byte] By [
Devz] at [2007-12-22]
The easiest way around this that I've found (if you have any control over the HTML that you're pumping in to the control) is to inject a <base> tag into the header of the document, like this:
<base href=\"path_to_css_file\" />
This means you can refer to the css file simply by filename, and any relative links within the HTML will work, too.
If you aren't able to put this into the HTML ... perhaps there's a way to programatically tell the WebBrowser object, or its Document, what the base URL is.
Hi Devz,
To reference your stylesheet without having to put in a literal path, use Application.StartupPath. This gives the path of the running application (meaning in your case it would give you the path of the Debug folder.) Here's an example that references a file called "test.css" in the directory of the executing app:
| | webBrowser1.DocumentText = "<html><head><link rel='stylesheet' type='text/css' href='" + Application.StartupPath + "/test.css' /></head><body> ... </body></html>"; |
You can also use Application.StartupPath as a root to reference files in subdirectories, for instance in your situation, if the file was located in a subdirectory of Debug called "Resources", replace "/test.css" with "/Resources/test.css" and so on.
Let me know if this helps.
Scott Morrison
WebBrowser PM
Microsoft Corp.