none
code-behind hyperlink color change when clicked RRS feed

  • Question

  • Web page using Site.Master has links defined in the master page, they work fine and can change the color using Site.css when clicked.

    Content page has table with row and cell. Cell contains a Control that is a Hyperlink. I want to change the color of the hyperlink when it is clicked. The Stye.css page has no effect. Hyperlink has no click event, neither does the Contol object and the Hyperlink.Style is read-only. This is ALL done in code behind. I set the forecolor of the link in code, no problem.

    How to tell the link to change the forecolor after it is clicked - without impacting the links defined in the Site.Master page? This does not use WPF, only WCF.

    Monday, August 13, 2012 5:42 PM

Answers

  • Create a CSS file as mentioned by Dragan ...

    then set the CssClass property of your Hyperlink object in the code behind.

    See the below example

    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
        void Button1_Click(object sender, EventArgs e)
        {
            if (HyperLink1.CssClass == "CssStyle1")
                HyperLink1.CssClass = "CssStyle2";
            else
                HyperLink1.CssClass = "CssStyle1";
        }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head2" runat="server">
        <title>CssClass Property Example</title>
        <style type="text/css">
            .CssStyle1   
            { 
               font: 10pt Verdana; 
               font-weight:700;
               color: Green;
            }
            .CssStyle2
            { 
               font: 15pt Times; 
               font-weight:250;
               color: Blue;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <h3>CssClass Property of a Web Control</h3>
            <asp:HyperLink id="HyperLink1" 
                NavigateUrl="http://www.microsoft.com" 
                CssClass="CssClass1" 
                Text="Click here to go to the Microsoft site" 
                Target="_new" runat="server" />
            <p><asp:Button id="Button1" 
                Text="Click to change the CSS style of the link"
                OnClick="Button1_Click" runat="server" />
             </p>
        </div>
        </form>
    </body>
    </html>


    Tanvir Huda Application Architect/Consultant http://thetechnocrate.wordpress.com/

    Wednesday, August 15, 2012 11:33 PM

All replies

  • Thanks for the idea, but the hyperlink is defined in the code behind as a new object, then added to the table cell where it is used. I'm not sure how to implement your idea. It looks like this:

    Hyperlink hy = new Hyperlink();

    ht.Text = "otherPage";

    hy.NavigateUrl = "~/OtherPage.aspx";

    tablecell.Controls.Add(hy);

    adding the class script to the Site.css styles file resulted in problems getting the data from the server. Weird!

    Thanks again.

    Wednesday, August 15, 2012 3:44 PM
  • Create a CSS file as mentioned by Dragan ...

    then set the CssClass property of your Hyperlink object in the code behind.

    See the below example

    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
        void Button1_Click(object sender, EventArgs e)
        {
            if (HyperLink1.CssClass == "CssStyle1")
                HyperLink1.CssClass = "CssStyle2";
            else
                HyperLink1.CssClass = "CssStyle1";
        }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head2" runat="server">
        <title>CssClass Property Example</title>
        <style type="text/css">
            .CssStyle1   
            { 
               font: 10pt Verdana; 
               font-weight:700;
               color: Green;
            }
            .CssStyle2
            { 
               font: 15pt Times; 
               font-weight:250;
               color: Blue;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <h3>CssClass Property of a Web Control</h3>
            <asp:HyperLink id="HyperLink1" 
                NavigateUrl="http://www.microsoft.com" 
                CssClass="CssClass1" 
                Text="Click here to go to the Microsoft site" 
                Target="_new" runat="server" />
            <p><asp:Button id="Button1" 
                Text="Click to change the CSS style of the link"
                OnClick="Button1_Click" runat="server" />
             </p>
        </div>
        </form>
    </body>
    </html>


    Tanvir Huda Application Architect/Consultant http://thetechnocrate.wordpress.com/

    Wednesday, August 15, 2012 11:33 PM