2

Possible Duplicate:
Is there a CSS parser for C#?

I'd like to access some CSS properties of my website defined in an external .css file at runtime. I've found that there is a way to programmatically set css properties in the codebehind, but I haven't come across any way to read the ones that are already defined. (I'm using C#)

Community
  • 1
  • 1
Valyrion
  • 2,342
  • 9
  • 29
  • 60
  • You will have to parse the css file itself. – NotMe May 04 '11 at 14:21
  • 2
    "How can I read CSS properties in ASP.NET?" doesn't translate to "Is there a CSS parser for C#?" Just because the answer may be to use a parser, doesn't make this a duplicate question. – Paul Turner May 04 '11 at 14:33

3 Answers3

4

You can read them "at run time" only using client side script.

With jQuery it's really simple, plus you can then send the value to the server using AJAX then handle it or store it for later use.

If it's valid option let me know and I can post basic example of what I mean.

Basic example

First, the HTML used:

<div class="mydiv" id="testDiv">I'm red</div>
<button type="button" id="testButton">Test</button>

CSS:

<style type="text/css">
.mydiv { background-color: red; }
</style>

Now you have to include jQuery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

And finally have this JS code:

<script type="text/javascript">
$(document).ready(function() {
    $("#testButton").click(function() {
        var oDiv = $("#testDiv");
        var sColor = oDiv.css("background-color");
        $.get("TestZone.aspx?cssvalue=" + sColor);
        alert("value has been sent");
    });
});
</script>

This will send the runtime background color of the test div to the server when the button is clicked, and in the code behind of TestZone.aspx you can handle the value. The example send it over the querystring you can same way send it as POST data if you prefer.

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

This is the way to get the display value for aWebControl in the code behind:

aWebControl.Style["display"]
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
0

ASP.NET only deals with the markup of the page itself.

You can programmatically get or set inline styles (CSS declared inline with an element using the style attribute) using the WebControl.Style property. This gives you a collection of name/value pairs that represents the inline style for that element.

control.Style["font-family"] = "Verdana";

Inline styles are considered pretty unpleasant, so don't use them unless you have a really good reason.

Anything outside of the page, you cannot access with ASP.NET natively. Essentially, the external stylesheets are only loaded by the browser rendering the page and aren't handled by the ASP.NET runtime, so you don't get a chance to inspect them.

If you want to interpret those files, you will need to load them manually and parse them with a CSS parser. Have a look at this question for possible solutions:

Community
  • 1
  • 1
Paul Turner
  • 38,949
  • 15
  • 102
  • 166