Search code examples
c#asp.netmaster-pages

How to determine which content page requested in Master page


How I can determine which content page is requested from the master page's load method? For example I have three content pages that use a master page. How I can determine which of these three pages requested by user from the code behind section of the master page?


Solution

  • You can determine the content page from your master page by using the ContentPlaceHolder's Page property. The following code assumes that the content place holder on your master page is called MainContent.

    // Page_Load in your master page code behind file
    protected void Page_Load(object sender, EventArgs e)
    {
      if (this.MainContent.Page is _Default)
      {
        // The default page
      }
    
      if (this.MainContent.Page is About)
      {
        // The About page.
      }
    }