0

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?

1
  • Try as I might I can't make sense of this question. What does 'add an additional class for this object' mean? What does 'having a preset class on the object' mean? Maybe some sample code would help? Commented Jul 20, 2010 at 3:31

3 Answers 3

1
txtField.CssClass = "first_class";
// Output: class="first_class"

txtField.CssClass += " another_class";
// Output: class="first_class another_class"
Sign up to request clarification or add additional context in comments.

1 Comment

This bloody rocks!!! I just read this is called a Lambda expression, introduced in 3.5, I'm using it!
1

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

Copying directly from another SO user's post without attribution. Not good.
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
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.
0

this didn't work

txtSubject.CssClass.Replace("text-single", "text-single subjectDisabled");

found the solution here:

Best way to change CSS Classes from code

1 Comment

this solution is out-dated for me now... see meep's example of Lambda expression above, which is very similar to JavaScript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.