When I navigate on a website utilizing MasterPages, does the application know what page I am on? If so, does it store it in an object I can access?

The reason I am asking is so I can replace this:

<div id="nav_main">
   <ul><asp:ContentPlaceHolder ID="navigation" runat="server">                    

//content page(s)
<asp:Content ContentPlaceHolderID="navigation" ID="theNav" runat="server">
   <li><a href="default.aspx">Home</a></li>
   <li id="current"><a href="faq.aspx">FAQ</a></li>
   <li><a href="videos.aspx">Videos</a></li>
   <li><a href="#">Button 4</a></li>
   <li><a href="#">Button 5</a></li>

With a more elegant solution for the navigation, which highlights the link to the page by having the list item's ID set to "current". Currently each page recreates the navigation with its respective link's ID set to current.

+1  A: 

You should be able to get the page by accessing the Page property. IE:

string type = this.Page.GetType().Name.ToString();

Wouldn't it be easier to navigate to Pages and ask those pages what's their Masterpage?

just a thought

There is only one masterpage, so I already know that information. thanks for your input though.
I don't understand how you navigate to masterpages then... I guess I'm missing something

There's also the Request.RawURL

+1  A: 

To get the current request URL from within the master page you would do:

string s = this.Page.Request.FilePath; // "/Default.aspx"

I also recommend moving your navigation into the master page instead of the content page. This will make it easier to maintain / access.


The navigation control, not the master page, should be in charge of what page is currently highlighted.

Either the page that is loaded should notify the navigation item who it is, or the nav control itself should keep track of it.

The point is that master pages are supposed to simply be a holder that content is displayed in. They aren't supposed to control anything.

Chris Lively
I am not using a navigation control, it is just a styled list.

You'd probably just use one of the Request path from within the master page to set the current. I'd probably also have a property on the master page to override it, so that pages without links or something could set it to something reasonable.

Mark Brackett
+1  A: 

I'd concur with Chris: use a control to handle display of this menu and make it aware of what link should be highlighted. Here's a method I use regularly. It may become more complex if you've got multiple pages that would need the same link styled differently, but you get the idea.

Dim thisURL As String = Request.Url.Segments(Request.Url.Segments.Count - 1)
Select Cast thisUrl
   Case "MenuItem1.aspx"
       lnkMenu1.CssClass = "Current"
   Case "MenuItem2.aspx"
       lnkMenu2.CssClass = "Current"
End Select
Thanks! Your code gave me an idea for my final solution to the problem.
Happy to help :)