9

I add a css class to my li tag like following:

liComPapers.Attributes.Add("class", "NoDisplay");

Is there any way to remove this specific class (NoDisplay) from that li tag somewhere else in my code?

I have tried the following code but it is not working.

liComPapers.Attributes["class"] = ""; 

Thanks

0

6 Answers 6

8

I've just made a sample to test your code, and found that the following part will do exactly what you want:

 var newClassValue = liTest.Attributes["class"].Replace("NoDisplay", "");
 liTest.Attributes["class"] = newClassValue;

Tested and Working: if (for some reason) the above code didn't work, I would recommend another approach, which is similar to the previous, with another way to replace the class value

var newClassValue = liTest.Attributes["class"].Replace("NoDisplay", "");
liTest.Attributes.Remove("class");
liTest.Attributes.Add("class",newClassValue);
Sign up to request clarification or add additional context in comments.

Comments

7

If I understand correctly:

If you wish to remove only NoDisplay, you could replace that part of the string with an empty string:

liComPapers.Attributes["class"] = liComPapers.Attributes["class"].Replace("NoDisplay", "");

However, .Add("class", "NoDisplay") won't add a new class to your class attribute. It will create a new class attribute with the value NoDisplay. Therefore if your markup is currently:

<li class="myClass"></li>

It would become:

<li class="myClass" class="NoDisplay"></li>

This is invalid markup.

To append new classes to an element with existing classes, you can do:

liComPapers.Attributes["class"] += " NoDisplay";

This would then render:

<li class="myClass NoDisplay"></li>

1 Comment

you gt it slightly faster than me :)
2
liComPapers.Attributes.Remove("class");

we can remove the CSS attribute for the Particular li tag

Comments

2

Suggested method

liComPapers.Attributes["class"] = liComPapers.Attributes["class"].Replace("NoDisplay", "");    

will cut another css classes that contains string "NoDisplay" and will cause errors

E.g.

<li class="NoDisplay AnotherClass-NoDisplay"></li>

becomes

<li class=" AnotherClass-"></li>

So, more safely solution would be

liComPapers.Attributes["class"] = String.Join(" ", liComPapers.Attributes["class"]
                                        .Split(' ')
                                        .Where(x => x != "NoDisplay")
                                        .ToArray());

Comments

1

Try the following:

liComPapers.Attributes.Remove("class");

AttributeCollection.Remove Method

Comments

0

Your code seems right. Check this link: How to: Set HTML Attributes for Controls in ASP.NET Web Pages

Are you using callbacks/ajax? Maybe you are not considering that..

Try to do a simple page with only one control and put a button to do a postback , on the button's click event (server side) assign the attribute the same way you did before. It should works.

Comments

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.