Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a MasterPage know what page is being displayed?

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.

like image 514
Anders Avatar asked Oct 09 '08 20:10

Anders


4 Answers

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
like image 128
Blumer Avatar answered Sep 20 '22 17:09

Blumer


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.

like image 22
Jared Avatar answered Sep 22 '22 17:09

Jared


Yes, Use the below code in your master file. It will give you the content page name.

Page.ToString().Replace("ASP.","").Replace("_",".")
like image 31
Sarim Shekhani Avatar answered Sep 20 '22 17:09

Sarim Shekhani


Alternatively you can search for page title if you have set an specific title to the child page instead of masterpage try:

this.Page.Title

Hope it helps.

like image 29
eblancode Avatar answered Sep 20 '22 17:09

eblancode