Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add style from code behind?

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?

like image 894
Manish Avatar asked Jan 05 '10 08:01

Manish


People also ask

How can we call CSS class in code behind in asp net?

If you want to add attributes, including the class, you need to set runat="server" on the tag. Thanks, I was sure it would be this simple. @Tyler, no. This adds a new class name to the control.


2 Answers

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";
like image 85
Mostafa Elmoghazi Avatar answered Oct 09 '22 00:10

Mostafa Elmoghazi


Use

HyperLink hlRow = new HyperLink();
hlRow.Attributes.Add("Style", "color:#000000");
like image 34
bydoga Avatar answered Oct 09 '22 00:10

bydoga