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?
A:
this didn't work
txtSubject.CssClass.Replace("text-single", "text-single subjectDisabled");
found the solution here:
http://stackoverflow.com/questions/445967/c-best-way-to-change-css-classes-from-code
Brandon
2010-07-20 03:30:23
this solution is out-dated for me now... see meep's example of Lambda expression above, which is very similar to JavaScript
Brandon
2010-07-20 23:05:08
+1
A:
txtField.CssClass = "first_class";
// Output: class="first_class"
txtField.CssClass += " another_class";
// Output: class="first_class another_class"
meep
2010-07-20 07:03:29
This bloody rocks!!! I just read this is called a Lambda expression, introduced in 3.5, I'm using it!
Brandon
2010-07-20 22:59:51
A:
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");
geocine
2010-07-20 07:46:38
Copying directly from another SO user's post without attribution. Not good.
Noldorin
2010-07-20 07:49:47
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
geocine
2010-07-20 07:52:10
I'll give you benefit of the doubt, though it's likely you did originally get it from AnthonyWJones's post <http://stackoverflow.com/questions/445967/c-best-way-to-change-css-classes-from-code> - easy enough to forget though.
Noldorin
2010-07-20 07:55:38