Does a MasterPage know what page is being displayed?

前端 未结 12 1752
無奈伤痛
無奈伤痛 2021-01-02 11:53

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

12条回答
  •  情深已故
    2021-01-02 12:18

    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;
                    }
                }
    
            }
        }
    

提交回复
热议问题