0

I have an ASP.NET/C# page that uses jQuery to load a Navigation.aspx page that contains my header and navigation buttons:

<script>
    $(function(){
        $("#nav-placeholder").load("Navigation.aspx");
    });
</script>

In the code behind of the Navigation.aspx page, I have a block of code that needs to get the name of the page that it's being loaded by. What I'm trying to do is highlight the navigation button of the current page. So, if I have a Manufacturing.aspx page that loads the Navigation.aspx page, I need to see the calling page's name in order to highlight the corresponding button, like so:

string pageName = Path.GetFileName(Request.Path);
   
    if (pageName == "Manufacturing.aspx")
    {
        btnManufacturing.Attributes.CssStyle.Add("background-color", "deepskyblue");
    }

The sample above retrieves the Navigation.aspx page name, not Manufacturing.aspx. Does anyone know of a way to accomplish this?

I might be able to do the same thing by accessing the items on the loaded page from the Manufacturing page and then updating the CSS in the code behind, but I haven't found a way to access those items either (for example, the btnManufacturing item in Navigation.asp). Thanks in advance!

  • You don't say if you are on MVC or WebForms but `Request.Url` should do the trick on webforms & maybe this on MVC https://stackoverflow.com/a/1278899/13798797 – Salik Rafiq Sep 18 '20 at 16:42
  • Thanks for the suggestion, I'm using Web Forms. I tried that, but it's still giving me the name of the page I'm loading, not the page I'm loading it from. – Wonderland Alice Sep 18 '20 at 16:59

1 Answers1

1

Just to update for anyone who might be looking for the answer, I found it. I needed to use Request.UrlReferrer. The code block below is what I added to Page_Load in the code behind on the page I'm loading (Navigation.aspx). It returns only the filename of the page that's loading it (Manufacturing.aspx).

    Uri MyUrl = Request.UrlReferrer;
    string pageName = System.IO.Path.GetFileName(Request.UrlReferrer.LocalPath); 
    
    if (pageName == "Manufacturing.aspx")
    {
        btnManufacturing.Attributes.CssStyle.Add("background-color", "deepskyblue");
    }