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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can replace “required” with an empty string:
lblName.CssClass = lblName.CssClass.Replace("required", "");
Method 2
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.
public void RemoveCssClass(WebControl controlInstance, String css)
{
controlInstance.CssClass = String.Join(" ", controlInstance.CssClass.Split(' ').Where(x => x != css).ToArray());
}
Method 3
This worked for me
lblName.CssClass = "regular";
Method 4
Use this:
object.CssClass= object.CssClass.Replace("MyClass", "");
Method 5
Here is my code inspired by @KevD’s code sample.
public static void RemoveCssClass(this HtmlGenericControl controlInstance, string css)
{
var strCssClass = controlInstance.Attributes["class"];
controlInstance.Attributes["class"] = string.Join(" ", strCssClass.Split(' ').Where(x => x != css).ToArray().Distinct());
}
public static void AddCssClass(this HtmlGenericControl controlInstance, string css)
{
var strCssClass = controlInstance.Attributes["class"];
var cssList = strCssClass.Split(' ').ToArray().Distinct();
cssList= cssList.Append(css);
controlInstance.Attributes["class"] = string.Join(" ", cssList);
}
/// <summary>
/// Add or remove specific css class
/// </summary>
/// <param name="controlInstance">Control to which css is to be added or remove</param>
/// <param name="css"> Css class name to be added</param>
/// <param name="bAddClass"> True to Add / false to remove</param>
public static void AddOrRemoveCssClass(this HtmlGenericControl controlInstance, string css, bool bAddClass)
{
if (bAddClass)
{
controlInstance.AddCssClass(css);
}
else
{
controlInstance.RemoveCssClass(css);
}
}
public static void RemoveCssClass(this WebControl controlInstance, string css)
{
controlInstance.CssClass = string.Join(" ", controlInstance.CssClass.Split(' ').Where(x => x != css).ToArray().Distinct());
}
public static void AddCssClass(this WebControl controlInstance, string css)
{
var cssList = controlInstance.CssClass.Split(' ').ToArray().Distinct();
cssList= cssList.Append(css);
controlInstance.CssClass = string.Join(" ", cssList);
}
/// <summary>
/// Add or remove specific css class
/// </summary>
/// <param name="controlInstance">Control to which css is to be added or remove</param>
/// <param name="css"> Css class name to be added</param>
/// <param name="bAddClass"> True to Add / false to remove</param>
public static void AddOrRemoveCssClass(this WebControl controlInstance, string css, bool bAddClass)
{
if (bAddClass)
{
controlInstance.AddCssClass(css);
}
else
{
controlInstance.RemoveCssClass(css);
}
}
Method 6
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.
Method 7
lblName.Attributes.Add("class","urclassname"); //add class to lblName
Method 8
To remove css Class from Code Behind
lblName.Attributes["class"]=" ";
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0