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"

Share:
111,578

Related videos on Youtube

Manish
Author by

Manish

Updated on July 05, 2022

Comments

  • Manish
    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
    cllpse over 14 years
    Hover is a pseudo-class, not a selector; this is a selector: ul li a { ... }
  • David Hedlund
    David Hedlund over 14 years
    well 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
    Manish over 14 years
    Thanks. BTW, I do have the aspx page, I can directly write the CSS class over there instead of using LiteralControl! :-)
  • Noon Silk
    Noon Silk over 14 years
    I'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
    Itay Levin over 13 years
    Hey 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
    Abhishek Shrivastava over 11 years
    Wonderful 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
    Markus Weninger about 6 years
    This has nothing to do with hover.