views:

1105

answers:

7

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:

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

//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>
</asp:Content>

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();
Jared
A: 

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

just a thought

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

There's also the Request.RawURL

Kon
+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.

Jared
A: 

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.
Anders
A: 

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
Blumer
Thanks! Your code gave me an idea for my final solution to the problem.
Anders
Happy to help :)
Blumer