Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can i read the content of a css class runtime in code behind?

Tags:

c#

asp.net

i have a page with a .css file linked to it. Let's say there is this content in it:

.wrapper {
 color:red;
}

is there a way to read the value of the .wrapper element runtime in the code behind?

the problem is that runtime a different stylesheet is linked so the .wrapper is always available, but the content is always different.

what i want to accomplish is to get the value (in this case: color:red;) from the currently attached stylesheet.

Is that possible?

EDIT: is it possible client side, then i can put it in a hidden field somehow

like image 468
Michel Avatar asked Nov 17 '11 10:11

Michel


1 Answers

I'm assuming that you will have the path (physical or virtual) to the CSS file you are linking to.

If it is a file on the webserver, you can load this into a text file, or if it is on a remote server you can use an XMLHTTPRequest to go and get it.

Once you have the body of the file loaded, the following reg-ex will parse the file, detect the wrapper class and pluck out the value of the color key into the first group of the match.

\.wrapper[\s|\S]*color\s*:\s*([^;]*);

Some sample code

    var inputfileReader = new StreamReader("path to my css file");
    string inputLine = inputfileReader.ReadToEnd() ;
    inputfileReader.Close();

    Regex cssParser = new Regex(@"\.wrapper[\s|\S]*color\s*:\s*([^;]*);");

    string myCssClass = string.Empty; ;
    if (cssParser.Match(inputLine).Groups[1] != null)
        myCssClass = cssParser.Match(inputLine).Groups[1].Value;
like image 145
John Perrin Avatar answered Oct 17 '22 14:10

John Perrin