简体   繁体   中英

How to determine which content page requested in Master page

How I can determine which content page requested from master page load method?for ezample I have content pages that use a master page.How I can determine which of these three pages requsted by user from master page?

thanks

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

A master page is actually a subclass of System.Web.UI.UserControl and as such it has a reference to the page through its Page property . For example, if you want to know the ASPX file and/or its folder, you can use the following code:

protected void Page_Load(object sender, EventArgs e)
{
  // Something like ~/Folder/Default.aspx
  string file = Page.AppRelativeVirtualPath;

  // Something like ~/Folder/
  string folder = Page.AppRelativeTemplateSourceDirectory;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM