How to add style from code behind?
111,578
Solution 1
: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.
You can add a class to your link: hlRow.CssClass = 'abc';
And define your class as such:
a.abc:hover {
...
}
Solution 2
You can use the CssClass property of the hyperlink:
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";
Solution 3
Use
HyperLink hlRow = new HyperLink();
hlRow.Attributes.Add("Style", "color:#000000");
Solution 4
Try this:
Html Markup
<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");
}
Solution 5
Also make sure the aspx page has AutoEventWireup="true"
and not AutoEventWireup="false"
Related videos on Youtube
Author by
Manish
Updated on July 05, 2022Comments
-
Manish almost 2 years
I want to add a style
A:Hover
to a HyperLink control from code behind.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? -
cllpse over 14 yearsHover is a pseudo-class, not a selector; this is a selector:
ul li a { ... }
-
David Hedlund over 14 yearswell yes, part of a selector is what i meant to convey: still in the sense that trying to add it to an element doesn't make sense, as it's rather used to find matching elements. but yeah, thanks for the remark
-
Manish over 14 yearsThanks. BTW, I do have the aspx page, I can directly write the CSS class over there instead of using LiteralControl! :-)
-
Noon Silk over 14 yearsI'd love to upvote you but for some crazy reason my vote limit is reached for "today" (which is not my today, but the US today) so I can't. So instead I'll leave this comment. And maybe when my ban on voting is lifted I can remember this post and come back and vote it :)
-
Itay Levin over 13 yearsHey guys, i have a follow up questions, what if i have a menu Item in the aspx code, that i want to assign to him the CSS class. how can i reach this property which is a webcontrol property and missing from the MenuItem Control? im trying to do a simple thing which is to create style for "Selected" mode...so the user will know which page is the current active page. any way to accomplish that?
-
Abhishek Shrivastava over 11 yearsWonderful trick. To make it more dynamic, put this under a method and replace class name with a token and user String.Format to dynamically populate it.
-
Markus Weninger about 6 yearsThis has nothing to do with hover.