Get Name of Page That Calls Another Page Loaded By JQuery

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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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");
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x