如何从代码中添加样式?

时间:2022-08-25 22:04:57

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

我想添加一个样式A:从后面的代码悬停到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:Hover for the hyperlink control? Do I need to define a class and associate that class with this control, if yes how?

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

8 个解决方案

#1


2  

:hover is 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 {
    ...
}

#2


26  

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";

#3


9  

Use

使用

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

#4


5  

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");
}

#5


2  

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

还要确保aspx页面有AutoEventWireup =“true”而不是AutoEventWireup =“false”

#6


1  

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";

#7


0  

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; }

#8


-2  

try this

尝试这个

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

#1


2  

:hover is 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 {
    ...
}

#2


26  

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";

#3


9  

Use

使用

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

#4


5  

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");
}

#5


2  

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

还要确保aspx页面有AutoEventWireup =“true”而不是AutoEventWireup =“false”

#6


1  

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";

#7


0  

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; }

#8


-2  

try this

尝试这个

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