tags:

views:

63

answers:

3

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
this solution is out-dated for me now... see meep's example of Lambda expression above, which is very similar to JavaScript
Brandon
+1  A: 
txtField.CssClass = "first_class";
// Output: class="first_class"

txtField.CssClass += " another_class";
// Output: class="first_class another_class"
meep
This bloody rocks!!! I just read this is called a Lambda expression, introduced in 3.5, I'm using it!
Brandon
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
Copying directly from another SO user's post without attribution. Not good.
Noldorin
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
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