0

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

4 Answers 4

1

The simple way would be to apply the default style, and then the pageA style. The browser should ignore the pageA declaration if it doesn't exist.
However I think the Right Thing™ would be to handle this in ASP or use separate files as Seb suggests.

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

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.