简体   繁体   English

在后面的代码中更改 css 类

[英]change css class in code behind

I'm trying to do something simple (in c#), but it doesn't work.我正在尝试做一些简单的事情(在 c# 中),但它不起作用。

What I want to achieve: Change the text color of a label by changing the css class.我想要实现的目标:通过更改 css 类来更改标签的文本颜色。

What I've got so far:到目前为止我所得到的:

CSS: CSS:

.feedback {
    font-family: inherit;
    font-weight: normal;
    font-style: italic;
    font-size: 10pt;
}
.feedback.success {
    color: #71d398;
}
.feedback.fail {
  color: #75b9e6;
}

C#: C#:

(...)
FeedbackLabel.Attributes.Add("Class", ".feedback.fail"); //one way I tried
FeedbackLabel.Text = "Error";
(...)
FeedbackLabel.CssClass = ".feedback.success"; //the other way I tried
FeedbackLabel.Text = "Done";
(...)

My problem: it gets the class, but it doesn't have any effect on the font.我的问题:它得到了类,但它对字体没有任何影响。 I even tried setting the .feedback.fail as default on the label and only change it in case of success.我什至尝试将 .feedback.fail 设置为标签上的默认值,只有在成功时才更改它。 The result was that after start the .feedback.fail was active (including all settings), but when it should have changed to .feedback.success it just reset to application default.结果是启动后 .feedback.fail 处于活动状态(包括所有设置),但是当它应该更改为 .feedback.success 时,它只是重置为应用程序默认值。 (But in debugging, I saw that it really got the right class..) (但在调试中,我看到它确实得到了正确的类..)

Do you have any idea what's interfering or what's wrong with my code above?你知道我上面的代码有什么干扰或有什么问题吗? Thanks in advance!提前致谢!

try this way FeedbackLabel.Attributes.Add("Class", "feedback fail") .试试这种方式FeedbackLabel.Attributes.Add("Class", "feedback fail")

you shouldn't use .你不应该使用. for specifying class attribute value for element用于指定元素的类属性值

Unfortunately no answer here was working for me, so I decided to provide my own answer to this problem:不幸的是,这里没有答案对我有用,所以我决定为这个问题提供我自己的答案:

FeedbackLabel.Style["border"] = "1px solid black";

this would be an example on how to change individual style elements, but still not sure how to change a class.这将是一个关于如何更改单个样式元素的示例,但仍然不确定如何更改类。

Try this尝试这个

FeedbackLabel.Attributes.Remove("class");

if(succcess)
  FeedbackLabel.Attributes.Add("class","feedback success");
else
  FeedbackLabel.Attributes.Add("class","feedback fail");

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

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