views:

383

answers:

4

I control styles for a set of pages in a common utility method. What i would like to do is check the .css file to see if a css class exists; if it does then pick it, else pick the default.

say PageA.aspx and PageB.aspx both use styles.css that contains .default{...} if i wanted PageA.aspx to be styled differently, i would just add another entry in styles.css .PageA{....} etc. At runtime, it will search for a css class named PageB, since it does not find it, it will default to the default class.

Question is how do i check the .css file to find out if a particular css class exists in the code-behind.

Thanks

A: 

Many questions in SO ask for a particular solution but don't state the real problem behind, so it's difficult to answer properly.

But I suspect what you're asking could be achieved far easier by splitting styles in 3 different files and including just what you need:

  • styles.css: common styles for PageA and PageB
  • styleA.css: rules applying only for PageA
  • styleB.css: rules applying only for PabeB, but with the same name used in styleA.css

Then, you include styleA.css or styleB.css depending on which page you're showing and that's it.

If this doesn't answer your question, then you might post what's your real problem instead of the solution you're trying to code - which might not be the best solution.

BTW: I don't think there's a way to know which CSS classes are defined; you just set and unset them.

Seb
A: 

You could load the css file using a StreamReader (since it is a text file) and search it line-by-line using the String.IndexOf() method, but that wouldn't be very efficient for every page load.

NinjaBomb
A: 

Your question doesn't make a lot of sense, but since CSS is not a programming language, if you want to know what's in another file, you're going to have to do it somewhere else.

Azeem.Butt
A: 
Matt Willtrout