向 LinkButton 添加样式而不覆盖 css
因此,如果我在 .cs 代码中向 LinkButton 添加背景颜色等样式,它会覆盖我所拥有的适用于它的任何 css。
有什么方法可以添加样式而不是在后面的代码中替换它?谢谢! 我使用链接按钮作为菜单,因此活动的 linkButton 应该具有不同的背景颜色。 所以我的解决方案是,当用户单击事件处理程序中的链接按钮时,我会执行以下操作:
lnkView.BackColor = System.Drawing.Color.FromName("#369");
但是我在 css 中的悬停样式将不再起作用:
.navlist a:hover
{
color: #fff;
background-color: #369;
text-decoration: none;
}
在我的 aspx 中:
<ul class="navlist">
<li><asp:LinkButton ID="lnkView" runat="server">view</asp:LinkButton></li>
<li><asp:LinkButton ID="lnkCreateNew" runat="server">create new</asp:LinkButton></li>
</ul>
So it looks like if I add style such as background color to LinkButton in my .cs code, it overrides any css I have that applies to it.
is there any way to add style rather than replace it in my code behind? Thanks!
I am using link button as a menu, so active linkButton should have different background color.
so my solution was when the user clicks on the link button in my event handler I do something like:
lnkView.BackColor = System.Drawing.Color.FromName("#369");
But then my hover style which I have in my css will no longer work:
.navlist a:hover
{
color: #fff;
background-color: #369;
text-decoration: none;
}
in my aspx:
<ul class="navlist">
<li><asp:LinkButton ID="lnkView" runat="server">view</asp:LinkButton></li>
<li><asp:LinkButton ID="lnkCreateNew" runat="server">create new</asp:LinkButton></li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:你的问题不清楚,但你似乎误解了CSS。将
background-color
添加到style
属性不会导致它完全忽略任何 CSS 规则。相反,它将覆盖background-color
属性的任何 CSS 规则,但不会影响任何其他规则。如果您不想覆盖 CSS 规则中的
background-color
属性,请将!important
标志添加到:hover
,像这样:另外,更改颜色,以使变化显而易见。
或者,您可以使用背景颜色为
.navlist a:link .Active
添加新的 CSS 规则,然后在代码中添加Active
类。 (lnkView.CssClass += "Active"
)顺便说一句,您应该编写
Color.FromArgb(0x33, 0x66, 0x99),而不是调用
。Color.FromName
)EDIT: Your question is unclear, but you appear to be mis-understanding CSS. Adding
background-color
to thestyle
property will not cause it to completely ignore any CSS rules. Rather, it will override any CSS rules for thebackground-color
property, but will not affect any other rules.If you don't want to override the
background-color
property from the CSS rule, add the!important
flag to the CSS rule in:hover
, like this:Also, change the color so that the change wil be noticable.
Alternatively, you could add a new CSS rule for
.navlist a:link .Active
with your background color, then add theActive
class in code. (lnkView.CssClass += "Active"
)By the way, instead of calling
Color.FromName
, you should writeColor.FromArgb(0x33, 0x66, 0x99)
.任何内联样式将始终覆盖从文档头部或外部 css 文件继承的任何样式。唯一的其他选择是添加一个 JavaScript 函数,该函数在 DOM 就绪或窗口就绪事件后覆盖对象的样式。
Any inline styles will always override any inherited styles from the head of a document or an external css file. The only other option would be to add a javascript function that overrides the style of the object after DOM ready or Window ready event.
不确定您在这里的用途,因为您的问题不太清楚。然而,这也可能是一种选择。
有两种 css 样式:
然后在后面的代码中只需切换 CSSClass:
这会将类更改为事后您想要的任何样式。
Not sure of your use here, as your question isn't real clear. However this could be an option as well.
Have two css styles:
Then in your code behind just switch your CSSClass:
This would change the class to have whatever style you wanted after the fact.
如果我正确理解您想要什么,您希望根据链接按钮是否悬停在其上应用不同的样式?因此,既有您所拥有的样式,也有:
如果您想要第三种颜色,在访问链接后,用
a:visited 定义它。
这就是您想要的吗?If I understand what you want correctly, you want different styles applied based on whether the linkbutton is being hovered over? So, have the style you have but also have:
if you want a third color, for after a link is visited, define that with
a:visited.
Is that what you're after?