向 LinkBut​​ton 添加样式而不覆盖 css

发布于 2024-08-16 03:54:39 字数 740 浏览 4 评论 0原文

因此,如果我在 .cs 代码中向 LinkBut​​ton 添加背景颜色等样式,它会覆盖我所拥有的适用于它的任何 css。

有什么方法可以添加样式而不是在后面的代码中替换它?谢谢! 我使用链接按钮作为菜单,因此活动的 linkBut​​ton 应该具有不同的背景颜色。 所以我的解决方案是,当用户单击事件处理程序中的链接按钮时,我会执行以下操作:

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

℡Ms空城旧梦 2024-08-23 03:54:39

编辑:你的问题不清楚,但你似乎误解了CSS。将 background-color 添加到 style 属性不会导致它完全忽略任何 CSS 规则。相反,它将覆盖 background-color 属性的任何 CSS 规则,但不会影响任何其他规则。

如果您不想覆盖 CSS 规则中的 background-color 属性,请将 !important 标志添加到 :hover,像这样:

background-color: #369 !important;

另外,更改颜色,以使变化显而易见。

或者,您可以使用背景颜色为 .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 the style property will not cause it to completely ignore any CSS rules. Rather, it will override any CSS rules for the background-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:

background-color: #369 !important;

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 the Active class in code. (lnkView.CssClass += "Active")

By the way, instead of calling Color.FromName, you should write Color.FromArgb(0x33, 0x66, 0x99).

熟人话多 2024-08-23 03:54:39

任何内联样式将始终覆盖从文档头部或外部 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.

月光色 2024-08-23 03:54:39

不确定您在这里的用途,因为您的问题不太清楚。然而,这也可能是一种选择。

有两种 css 样式:

.navlistafteranaction a:link
    {
        color: #fff;
        background-color: #123;
        text-decoration: none;
    }

.navlist a:link
    {
        color: #fff;
        background-color: #123;
        text-decoration: none;
    }

然后在后面的代码中只需切换 CSSClass:

lnkbtn.CssClass = "navlistafteranaction";

这会将类更改为事后您想要的任何样式。

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:

.navlistafteranaction a:link
    {
        color: #fff;
        background-color: #123;
        text-decoration: none;
    }

.navlist a:link
    {
        color: #fff;
        background-color: #123;
        text-decoration: none;
    }

Then in your code behind just switch your CSSClass:

lnkbtn.CssClass = "navlistafteranaction";

This would change the class to have whatever style you wanted after the fact.

盛装女皇 2024-08-23 03:54:39

如果我正确理解您想要什么,您希望根据链接按钮是否悬停在其上应用不同的样式?因此,既有您所拥有的样式,也有:

.navlist a:link
    {
        color: #fff;
        background-color: #123;
        text-decoration: none;
    }

如果您想要第三种颜色,在访问链接后,用 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:

.navlist a:link
    {
        color: #fff;
        background-color: #123;
        text-decoration: none;
    }

if you want a third color, for after a link is visited, define that with a:visited. Is that what you're after?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文