Does a MasterPage know what page is being displayed?
Solution 1
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
Solution 2
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.
Solution 3
Yes, Use the below code in your master file. It will give you the content page name.
Page.ToString().Replace("ASP.","").Replace("_",".")
Solution 4
this is in C#
string thisURL = Request.Url.Segments[Request.Url.Segments.Length - 1];
if (thisURL.ToLower()== "default.aspx") li1.Attributes.Add("class","yekan active");
if (thisURL.ToLower() == "experts.aspx") li2.Attributes.Add("class", "yekan active");
Solution 5
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.
Anders
Currently freelancing my IT skills while working part-time as an IT consultant for a design firm. SOreadytohelp
Updated on July 10, 2022Comments
-
Anders almost 2 years
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.
-
Anders over 15 yearsI am not using a navigation control, it is just a styled list.
-
Admin over 9 yearsExplanation will help them a lot
-
Babak Naffas over 9 yearsThis solution would not work if you have two .aspx pages that share the same code behind.