Does a MasterPage know what page is being displayed?
Asked Answered
W

12

9

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.

Writ answered 9/10, 2008 at 20:26 Comment(0)
A
17

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
Abdias answered 9/10, 2008 at 21:19 Comment(0)
P
10

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.

Prewar answered 9/10, 2008 at 20:55 Comment(0)
W
2

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

Page.ToString().Replace("ASP.","").Replace("_",".")
Whitehot answered 8/7, 2013 at 11:56 Comment(0)
A
1

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.

Antoinetteanton answered 3/6, 2018 at 10:6 Comment(0)
A
1

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");
Adamsite answered 7/6, 2018 at 12:59 Comment(0)
P
0

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

string type = this.Page.GetType().Name.ToString();
Prewar answered 9/10, 2008 at 20:31 Comment(0)
U
0

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.

Unschooled answered 9/10, 2008 at 21:7 Comment(0)
P
0

It worked for me this way - Thanks Jared

This is what I did to get our nav menu to highlight the current menu item for the page that we are viewing. The code is in the master page. You basically get the filepath (Jared's way) We use the "~" in our links so I had to strip that out. Iterate the menuItems collection of the Menu control. Compare the navigateUrl property.

(I'm not the best coder and even worse at explaining - but it works and I was quite chuffed with it!)

protected void HighlightSelectedMenuItem()
    {
        string s = this.Page.Request.FilePath; // "/Default.aspx"
        string nav;
        if (s.Contains("~"))
        {
            s = s.Remove(s.IndexOf("~"), 1);
        }

        foreach (MenuItem item in navMenu.Items)
        {
            if (item.NavigateUrl.Contains("~"))
            {
                nav = item.NavigateUrl.Remove(item.NavigateUrl.IndexOf("~"), 1);
                if (s == nav)
                {
                    item.Selected = true;
                }
            }

        }
    }
Perch answered 6/5, 2014 at 11:32 Comment(0)
B
0
string s = this.Page.GetType().FullName;
string[] array = s.Split('_');
int count = array.Count<String>();
string currentPage = array[count - 2];
Behnken answered 4/10, 2014 at 5:54 Comment(2)
Explanation will help them a lotStultify
This solution would not work if you have two .aspx pages that share the same code behind.Ectopia
G
-1

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.

Gounod answered 9/10, 2008 at 20:56 Comment(1)
I am not using a navigation control, it is just a styled list.Writ
N
-1

try

this.Page.Master

It will get you the master page of the current page.

Neighborly answered 17/4, 2013 at 13:44 Comment(0)
M
-2

There's also the Request.RawURL

Molecular answered 9/10, 2008 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.