views:

754

answers:

4

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?

A: 

: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 {
    ...
}
David Hedlund
Hover is a pseudo-class, not a selector; this is a selector: `ul li a { ... }`
roosteronacid
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
David Hedlund
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?
Itay Levin
A: 

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";
SubPortal
Thanks. BTW, I do have the aspx page, I can directly write the CSS class over there instead of using LiteralControl! :-)
Manish
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 :)
Noon Silk
Thank you very much silky. Good luck.
SubPortal
A: 

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");
}
Mehdi Golchin
He he..nice and tricky one..!!
Manish
A: 

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:

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