Note that there are some explanatory texts on larger screens.

plurals
  1. POC# WebBrowser control not applying css
    primarykey
    data
    text
    <p>I have a project that I am working on in VS2005. I have added a WebBrowser control. I add a basic empty page to the control</p> <pre><code>private const string _basicHtmlForm = "&lt;html&gt; " + "&lt;head&gt; " + "&lt;meta http-equiv='Content-Type' content='text/html; charset=utf-8'/&gt; " + "&lt;title&gt;Test document&lt;/title&gt; " + "&lt;script type='text/javascript'&gt; " + "function ShowAlert(message) { " + " alert(message); " + "} " + "&lt;/script&gt; " + "&lt;/head&gt; " + "&lt;body&gt;&lt;div id='mainDiv'&gt; " + "&lt;/div&gt;&lt;/body&gt; " + "&lt;/html&gt; "; private string _defaultFont = "font-family: Arial; font-size:10pt;"; private void LoadWebForm() { try { _webBrowser.DocumentText = _basicHtmlForm; } catch(Exception ex) { MessageBox.Show(ex.Message); } } </code></pre> <p>and then add various elements via the dom (using _webBrowser.Document.CreateElement). I am also loading a css file:</p> <pre><code>private void AddStyles() { try { mshtml.HTMLDocument currentDocument = (mshtml.HTMLDocument) _webBrowser.Document.DomDocument; mshtml.IHTMLStyleSheet styleSheet = currentDocument.createStyleSheet("", 0); TextReader reader = new StreamReader(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"basic.css")); string style = reader.ReadToEnd(); styleSheet.cssText = style; } catch(Exception ex) { MessageBox.Show(ex.Message); } } </code></pre> <p>Here is the css page contents:</p> <pre><code>body { background-color: #DDDDDD; } .categoryDiv { background-color: #999999; } .categoryTable { width:599px; background-color:#BBBBBB; } #mainDiv { overflow:auto; width:600px; } </code></pre> <p>The style page is loading successfully, but the only elements on the page that are being affected are the ones that are initially in the page (body and mainDiv). I have also tried including the css in a element in the header section, but it still only affects the elements that are there when the page is created.</p> <p>So my question is, does anyone have any idea on why the css is not being applied to elements that are created after the page is loaded? I have also tried no applying the css until after all of my elements are added, but the results don't change.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COIve wondered the same. I always assumed that creating a webpage in that manner just created a stale/dead/static page, and that the rendering engine stopped checking styles unless an element within the page causes the renderer to "wake up". But im pulling that out of thin air. I just learned to add the CSS as the first thing to the head when initially creating a doc.
      singulars
    2. COI know that this probably doesn't help at all (which is why it's a comment), but I wanted to tell you from experience that C#'s WebBrowser component is junk. Seemingly trivial things tend to end up as massive headaches and when it doesn't do what you want, you often end up having to go to the underlying ActiveX component... which is a mess to say the least (~5 different interfaces completely unorganized, having arbitrary sets of functionality). I highly recommend a different browser component whenever possible, for example GeckoFX, based on the Firefox engine.
      singulars
    3. COThere's a key piece of information missing here: the elements you're creating, the ones that aren't getting styled. Presumably, you're setting the class attribute to `"categoryDiv"`, `"categoryTable"`, etc... But, that isn't explicitly stated in your question. Given the results you're describing, I'm far more inclined to believe there's a bug in your element creation than anything directly connected to your stylesheet creation. That being said, you do realize you're forcing the new stylesheet to be the first in the document (and therefore lowest-priority), right?
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload