How to get the value of a property of a CSS class in code

How can I get the value of a style defined in a CSS class?

The markup has:

CssClass="grdTextBox" Text="aaaaaaaabbbbbbbccccccc"

      

CSS Style:

.grdTextBox {FONT-SIZE: 12px; FONT-FAMILY: verdana; } 

      

The .cs file has:

string cssClass = txtComments.CssClass;
Response.Write(" cssClass is : " + cssClass);

      

How can I find the value for the font size or font family from code?

I can find them if they are in a style tag or are attributes of a textbox. But how can you find values ​​if they are defined in a CSS class?

0


a source to share


5 answers


No. For lack of a real explanation, CSS is a client side technology. The styles in the .css file are not directly related to the html element until the browser renders it.



+5


a source


I ran into the same problem and I think there might be some solution by opening the .css file and doing a manual search in the requested style, returning its contents.

This will require manual disassembly, and perhaps you have more than one .css file or complex styles that make this decision a little awful, but I think it's better than "impossible".



I would like to hear a more elegant solution.

+1


a source


You cannot do this.

The styles you declare in ASPX are concatenated with the output, but as far as I know, it doesn't appear in the code.

Only if you add other styles to the collection, such as MyControl.Style.Add ("font-size", "12px"), will you get it there.

0


a source


Me.Menu.Style("margin-top") = "170px"

      

You can change it if you want to use inline styling, otherwise you can write your own CSS type object to control the CSS.

You can actually create a library like this for people to download and contribute just by saying.

0


a source


You can do this with HtmlAgilityPack. Sample code:

var doc = new HtmlAgilityPack.HtmlDocument();
doc.Load("PageUrl");

IList<HtmlNode> nodes = doc.QuerySelectorAll("div .my-class[data-attr=123] > ul li");
HtmlNode node = nodes.QuerySelector("p.with-this-class span[data-myattr]");

      

0


a source







All Articles