C# 如何从后面的代码添加样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2004720/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:34:12  来源:igfitidea点击:

How to add style from code behind?

c#asp.netcsshyperlink

提问by Manish

I want to add a style A:Hoverto a HyperLink control from code behind.

我想A:Hover从后面的代码中向 HyperLink 控件添加样式。

I can do like this :

我可以这样做:

HyperLink hlRow = new HyperLink();
hlRow.Style.Add("color", "#000000");
hlRow.Style.Add("text-decoration", "none");

But how can I add styles for A:Hoverfor the hyperlink control? Do I need to define a class and associate that class with this control, if yes how?

但是如何A:Hover为超链接控件添加样式?我是否需要定义一个类并将该类与此控件相关联,如果是,如何?

采纳答案by dxh

:hoveris a selector, and not a style. What you're doing in your example is adding inline styles to an element, and a selector equivalent for that obviously doesn't make much sense.

:hover是一个选择器,而不是一个样式。您在示例中所做的是向元素添加内联样式,而与此等效的选择器显然没有多大意义。

You can add a class to your link: hlRow.CssClass = 'abc';And define your class as such:

您可以在链接中添加一个类:hlRow.CssClass = 'abc';并定义您的类:

a.abc:hover {
    ...
}

回答by Mostafa Elmoghazi

You can use the CssClass property of the hyperlink:

您可以使用超链接的 CssClass 属性:

LiteralControl ltr = new LiteralControl();
        ltr.Text = "<style type=\"text/css\" rel=\"stylesheet\">" +
                    @".d
                    {
                        background-color:Red;
                    }
                    .d:hover
                    {
                        background-color:Yellow;
                    }
                    </style>
                    ";
        this.Page.Header.Controls.Add(ltr);
        this.HyperLink1.CssClass = "d";

回答by Mehdi Golchin

Try this:

尝试这个:

Html Markup

Html 标记

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="#">HyperLink</asp:HyperLink>

Code

代码

using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

protected void Page_Load(object sender, EventArgs e)
{
    Style style = new Style();
    style.ForeColor = Color.Green;
    this.Page.Header.StyleSheet.CreateStyleRule(style, this, "#" + HyperLink1.ClientID + ":hover");
}

回答by Noon Silk

You can't.

你不能。

So just don't apply styles directly like that, and apply a class "foo", and then define that in your CSS specification:

所以不要像那样直接应用样式,而是应用一个类“foo”,然后在你的 CSS 规范中定义它:

a.foo { color : orange; }
a.foo:hover { font-weight : bold; }

回答by bat

Also make sure the aspx page has AutoEventWireup="true"and not AutoEventWireup="false"

还要确保 aspx 页面有AutoEventWireup="true"和没有 AutoEventWireup="false"

回答by bydoga

Use

HyperLink hlRow = new HyperLink();
hlRow.Attributes.Add("Style", "color:#000000");

回答by denjerlog

If no file available for download, I needed to disable the asp:linkButton, change it to grey and eliminate the underline on the hover. This worked:

如果没有可供下载的文件,我需要禁用 asp:linkBut​​ton,将其更改为灰色并消除悬停时的下划线。这有效:

.disabled {
    color: grey;
    text-decoration: none !important;
}

LinkButton button = item.FindControl("lnkFileDownload") as LinkButton;
button.Enabled = false;
button.CssClass = "disabled";

回答by vivek verma

try this

尝试这个

 lblMsg.Text = @"Your search result for <b style=""color:green;"">" + txtCode.Text.Trim() + "</b> ";