在代码后面删除css类

时间:2022-11-22 23:45:09

I have this control

我有这个控制

<asp:Label ID="lblName" runat="server" Text="My Name" CssClass="required regular" />

I want to remove the required class from code behind, how can I do that?

我想从后面的代码中删除必需的类,我怎么做呢?

6 个解决方案

#1


52  

You can replace "required" with an empty string:

您可以用空字符串替换“required”:

lblName.CssClass = lblName.CssClass.Replace("required", "");

#2


14  

Just a slightly more generic way of doing the same - should rule out potential errors where a css class might occur elsewhere in the CssClass property.

只需使用一种更通用的方法,就可以排除css类可能出现在CssClass属性其他地方的潜在错误。

public void RemoveCssClass(WebControl controlInstance, String css)
{
    controlInstance.CssClass = String.Join(" ", controlInstance.CssClass.Split(' ').Where(x => x != css).ToArray());
}

#3


1  

Use this:

用这个:

object.CssClass= object.CssClass.Replace("MyClass", "");

#4


1  

This worked for me

这为我工作

lblName.CssClass = "regular";

#5


0  

NOTE: whether you add or replace a css class in codeBehind, remember to include equivalent attributes in both classes i.e. both having background-color, font-family...etc. because otherwise you may be fooled to think that the class never switched even though it did but didn't update the equivalent attributes.

注意:无论您在codeBehind中添加或替换css类,请记住在两个类中都包含相同的属性,例如都具有背景色、字体族等等。因为否则,您可能会误以为即使类进行了切换但没有更新相应的属性,也不会切换类。

#6


0  

lblName.Attributes.Add("class","urclassname"); //add class to lblName

#1


52  

You can replace "required" with an empty string:

您可以用空字符串替换“required”:

lblName.CssClass = lblName.CssClass.Replace("required", "");

#2


14  

Just a slightly more generic way of doing the same - should rule out potential errors where a css class might occur elsewhere in the CssClass property.

只需使用一种更通用的方法,就可以排除css类可能出现在CssClass属性其他地方的潜在错误。

public void RemoveCssClass(WebControl controlInstance, String css)
{
    controlInstance.CssClass = String.Join(" ", controlInstance.CssClass.Split(' ').Where(x => x != css).ToArray());
}

#3


1  

Use this:

用这个:

object.CssClass= object.CssClass.Replace("MyClass", "");

#4


1  

This worked for me

这为我工作

lblName.CssClass = "regular";

#5


0  

NOTE: whether you add or replace a css class in codeBehind, remember to include equivalent attributes in both classes i.e. both having background-color, font-family...etc. because otherwise you may be fooled to think that the class never switched even though it did but didn't update the equivalent attributes.

注意:无论您在codeBehind中添加或替换css类,请记住在两个类中都包含相同的属性,例如都具有背景色、字体族等等。因为否则,您可能会误以为即使类进行了切换但没有更新相应的属性,也不会切换类。

#6


0  

lblName.Attributes.Add("class","urclassname"); //add class to lblName