I have an existing class for an input, is it possible to add an additional class for the object in C#.net, instead of doing a if/else and not having a preset class on the object in the first place?
3 Answers
txtField.CssClass = "first_class";
// Output: class="first_class"
txtField.CssClass += " another_class";
// Output: class="first_class another_class"
1 Comment
bcm
This bloody rocks!!! I just read this is called a Lambda expression, introduced in 3.5, I'm using it!
Try this, works for me.
public static class ControlUtility
{
public static void AddCssClass(WebControl control, string cssClass)
{
control.CssClass += " " + cssClass;
}
public static void RemoveCssClass(WebControl control, string cssClass)
{
control.CssClass = control.CssClass.Replace(" " + cssClass, "");
}
}
ControlUtility.RemoveCssClass(lnkletter, "ln-enabled");
ControlUtility.RemoveCssClass(lnkletter, "ln-disabled");
3 Comments
Noldorin
Copying directly from another SO user's post without attribution. Not good.
Aivan Monceller
hi, i am copying this directly from my code base. if you are doing the same it's not my fault. didn't know it existed here at stack overflow, I didn't clicked the last link
Noldorin
I'll give you benefit of the doubt, though it's likely you did originally get it from AnthonyWJones's post <stackoverflow.com/questions/445967/…> - easy enough to forget though.
this didn't work
txtSubject.CssClass.Replace("text-single", "text-single subjectDisabled");
found the solution here:
1 Comment
bcm
this solution is out-dated for me now... see meep's example of Lambda expression above, which is very similar to JavaScript