Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Changing currently selected hyperlink's color

0.00/5 (No votes)
8 May 2009 1  
Changing the currently selected hyperlink's color

Introduction

Playing with hyperlinks on a master page is not easy. Sometimes, you would like to change the colour of your links (i.e., the current page to have a different colour) using the session object. This tutorial shows you how to do this using C#. So, let's get it on the go.

Using the code

I am using Visual Studio 2008 on my machine. Now this also applies to VWD 2005, 2008 and VS 2005. First, create a new website and delete the default.aspx page created by Visual studio. Now, create a master page.

Add the following hyperlinks to the master page in design view:

  • Link 1
  • Link 2
  • Link 3

Now, we need to add code to our master page so that each time the link is selected, the colour changes. We need to check which link is selected and handle that with some code.

Now, let's look at the C# code:

if(!ispostback){if(Session["Clicked"]=="link1")
{lnk1.Style.Add("color", "#009900")}; if(Session["Clicked"]=="link2")
{lnk2.Style.Add("color", "#009900")}; if(Session["Clicked"]=="link3")
{lnk3.Style.Add("color", "#009900")};}

That is it. We can change the colour of the selected hyperlink using the master page. Now on each page, we need to change the Session["Clicked"] in the form_load method, like this:

if(!ispostback)
{
    Session["Clicked"]="link1";
}

The above code will change on each page.

Points of interest

You can use C# to do some markup.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here