Click here to Skip to main content
Click here to Skip to main content

Highlighting Current Page in ASP.NET Master Page

By , 13 May 2013
 

Introduction

In this tip, I am going to explore how to highlight the current page in ASP.NET master page. In earlier days, highlighting the current page in HTML, we used to add style class attribute in each page. In ASP.NET master page, it’s very easy to highlight the current page and it makes the developer's life easier. Follow the simple steps to highlight the current page in ASP.NET master page.

Step 1

In the master page, add two image buttons:

<form id="form1" runat="server">
    <div>

    <table border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td><asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/home.gif"
            BorderStyle="None" BorderWidth="0px" EnableViewState="False"
            onclick="ImageButton1_Click" /></td>
            <td><asp:ImageButton ID="ImageButton2"
            runat="server" ImageUrl="~/images/about.gif" BorderStyle="None"
            BorderWidth="0px" EnableViewState="False" onclick="ImageButton2_Click" /></td>
    </tr></table>


        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>

Step 2

In each content page, add the following code to access the master page controls:

<%@ MasterType VirtualPath=”~/MasterPage.master” %>

Step 3

Let’s add the following code in each content page Page Load to change the images dynamically.

Default.aspx

protected void Page_Load(object sender, EventArgs e)
    {
        ImageButton img = (ImageButton)this.Master.FindControl("ImageButton1");
        img.ImageUrl = "~/images/home-over.gif";

    }

Default2.aspx

protected void Page_Load(object sender, EventArgs e)
    {
        ImageButton img = (ImageButton)this.Master.FindControl("ImageButton2");
        img.ImageUrl = "~/images/about-over.gif";
    }

Look into the below images before applying and after applying the codes.

Before

After

Default.aspx
Default2.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ravindra T C
Software Developer Marlabs Software Pvt Ltd, Mysore
India India
Member
I'm a software developer. I spent most of the time in learning new technologies. I've a keen interest in client-side technologies especially JavaScript and Jquery it is the most beautiful language ever seen.

I like sharing my knowledge and written some non-popular articles. I believe in quality and standards but blames myself for lagging them.

I believe in small things and they makes me happy!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130513.1 | Last Updated 13 May 2013
Article Copyright 2013 by Ravindra T C
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid