简体   繁体   English

检查样式表文件中后面代码中的css类是否存在

[英]check if css class exists in the stylesheet file in code behind

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; 我想做的是检查.css文件,看看是否存在css类。 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. 如果我希望PageA.aspx和PageB.aspx使用不同的样式,则说PageA.aspx和PageB.aspx都使用包含.default {...}的styles.css,我只是在styles.css .PageA {....中添加另一个条目}等。在运行时,它将搜索名为PageB的CSS类,因为找不到它,所以它将默认为默认类。

Question is how do i check the .css file to find out if a particular css class exists in the code-behind. 问题是如何检查.css文件以查明背后的代码中是否存在特定的CSS类。

Thanks 谢谢

The simple way would be to apply the default style, and then the pageA style. 简单的方法是应用默认样式,然后应用pageA样式。 The browser should ignore the pageA declaration if it doesn't exist. 浏览器应该忽略pageA声明(如果不存在)。
However I think the Right Thing™ would be to handle this in ASP or use separate files as Seb suggests. 但是,我认为Right Thing™可以在ASP中处理此问题,也可以按照Seb的建议使用单独的文件。

Many questions in SO ask for a particular solution but don't state the real problem behind, so it's difficult to answer properly. SO中的许多问题都需要特定的解决方案,但没有说明真正的问题所在,因此很难正确回答。

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: 但是我怀疑,通过将样式分为3个不同的文件并仅包含所需的内容,可以轻松实现您的要求:

  • styles.css: common styles for PageA and PageB styles.css:PageA和PageB的常用样式
  • styleA.css: rules applying only for PageA styleA.css:仅适用于PageA的规则
  • styleB.css: rules applying only for PabeB, but with the same name used in styleA.css styleB.css:仅适用于PabeB的规则,但与styleA.css中使用的名称相同

Then, you include styleA.css or styleB.css depending on which page you're showing and that's it. 然后,根据要显示的页面而已,包括styleA.css或styleB.css。

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; 顺便说一句:我认为没有办法知道定义了哪些CSS类。 you just set and unset them. 您只需设置和取消设置它们。

您可以使用StreamReader加载css文件(因为它是一个文本文件),然后使用String.IndexOf()方法逐行搜索它,但这并不是每次页面加载都非常有效。

您的问题没有多大意义,但是由于CSS不是一种编程语言,因此,如果您想知道另一个文件中的内容,则必须在其他地方进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM