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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With