0

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

The markup has:

CssClass="grdTextBox" Text="aaaaaaaabbbbbbbccccccc"

The CSS style is:

.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 font size or font family from code behind?

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

5 Answers 5

5

You don't. For lack of a real explanation, CSS is a client side technology. The styles in the .css file aren't directly associated with the html element until the browser renders it.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm facing the same problem, and i think there can be a kind of solution by opening the .css file and searching manually for the style requested, returning its content.

It would require manual parsing, and perhaps you have more than one .css file, or complex styles that make this solution a bit horrible, but i think it's better than "not possible".

I would love to hear for a more elegant solution.

Comments

0

You cannot do it.

The styles you declare in ASPX are merged to the output but it is not visible in code behind as far as I know.

Only if you add another styles to the collection like MyControl.Style.Add ("font-size", "12px") then you will have it there.

Comments

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

You can change it if you want to use inline style otherwise you can write your own CSS type object to manipulate CSS.

Actually you can build a library like that so that people can download and contribute,just saying.

Comments

0

You can do it with HtmlAgilityPack. Example 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]");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.