C# 从后面的代码中删除 ASP.net 中的 <li> 标记中的 CSS 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13521257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing CSS class from <li> tag in ASP.net from code behind
提问by Sev
I add a css class to my li tag like following:
我将一个 css 类添加到我的 li 标签中,如下所示:
liComPapers.Attributes.Add("class", "NoDisplay");
Is there any way to remove this specific class (NoDisplay) from that li tag somewhere else in my code?
有什么方法可以从我代码中其他地方的 li 标签中删除这个特定的类 (NoDisplay)?
I have tried the following code but it is not working.
我已经尝试了以下代码,但它不起作用。
liComPapers.Attributes["class"] = "";
Thanks
谢谢
采纳答案by Mohammed Swillam
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);
回答by IrishChieftain
回答by Luferogo
Your code seems right. Check this link: How to: Set HTML Attributes for Controls in ASP.NET Web Pages
你的代码似乎是正确的。检查此链接:如何:为 ASP.NET 网页中的控件设置 HTML 属性
Are you using callbacks/ajax? Maybe you are not considering that..
你在使用回调/ajax 吗?也许你没有考虑那个..
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.
尝试做一个只有一个控件的简单页面,并放置一个按钮来进行回发,在按钮的单击事件(服务器端)上以与之前相同的方式分配属性。它应该有效。
回答by Curt
If I understand correctly:
如果我理解正确:
If you wish to remove only NoDisplay, you could replace that part of the string with an empty string:
如果您只想删除NoDisplay,您可以用空字符串替换字符串的那部分:
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 classattribute with the value NoDisplay. Therefore if your markup is currently:
但是,.Add("class", "NoDisplay")不会向您的类属性添加新类。它将创建一个class值为的新属性NoDisplay。因此,如果您的标记当前是:
<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>
回答by Boopathi.Indotnet
liComPapers.Attributes.Remove("class");
we can remove the CSS attribute for the Particular li tag
我们可以移除 Particular li 标签的 CSS 属性
回答by Seven
Suggested method
建议方法
liComPapers.Attributes["class"] = liComPapers.Attributes["class"].Replace("NoDisplay", "");
will cut another css classes that contains string "NoDisplay"and will cause errors
将剪切另一个包含字符串的 css 类"NoDisplay"并会导致错误
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());

