How to determine which Child Page is being displayed from Master Page?

若如初见. 提交于 2019-12-20 09:50:48

问题


I'm writing code on the master page, and I need to know which child (content) page is being displayed. How can I do this programmatically?


回答1:


This sounds like a bad idea to start with. The idea of the master is that it shouldn't care what page is there as this is all common code for each page.




回答2:


I use this:

string pageName = this.ContentPlaceHolder1.Page.GetType().FullName;

It retuns the class name in this format "ASP.default_aspx", but I find that easy to parse for most purposes.

Hope that helps!




回答3:


It's better to let the ContentPage notify the MasterPage. That's why the ContentPage has a Master Property and MasterPage does not have Child property. Best pratice in this is to define a property or method on the MasterPage and use this through the Master property of the ContentPage.

If you use this technique it's best to explicitly specify the classname for the MasterPage. This makes to use the MasterPage in the ContentPage.

Example:

//Page_Load
MyMaster m = (MyMaster)this.Master;

m.TellMasterWhoIAm(this);

Hope this helps.




回答4:


I have had a reason to check the child page in the master page.

I have all my menu options on my master page and they need to be disabled if certain system settings are not set up.

If they are not then a message is displayed and the buttons are disabled. As the settings page is a content page from this master page I don't want the message to keep being displayed on all the settings pages.

this code worked for me:

                //Only show the message if on the dashboard (first page after login)
                if (this.ContentPlaceHolder1.Page is Dashboard)
                {
                    //Show modal message box
                    mmb.Show("Warning Message");
                }



回答5:


Use the Below code.

Page.ToString().Replace("ASP.","").Replace("_",".")



回答6:


You can use:

Request.CurrentExecutionFilePath




回答7:


Here is my solution to the problem (this code goes into the code behind the master page):

if (Page.TemplateControl.AppRelativeVirtualPath == "~/YourPageName.aspx")
{
   // your code here
}

or a bit more sophisticated, but less readable:

if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/YourPageName.aspx", StringComparison.OrdinalIgnoreCase))
{
   // your code here
}



回答8:


Request.CurrentExecutionFilePath;

or

Request.AppRelativeCurrentExecutionFilePath;



回答9:


I do something similar to this in a project of mine to dynamically attach css files based on the page being loaded. I just get the name of the file from the request:

this.Request.Url.AbsolutePath

And then extract the file name from there. I'm not sure if this will work if you are doing URL re-writes though.




回答10:


You can do this by getting the last segmant or the request and I'll be the Form name

string pageName = this.Request.Url.Segments.Last(); 

if (pageName.Contains("EmployeeTermination.aspx"))
{

}



回答11:


You can try this one:

<%: this.ContentPlaceHolder1.Page.GetType().Name.Split('_')[0].ToUpper() %>

Put that code within the title tags of the Site.Master




回答12:


string s =   Page.ToString().Replace("ASP.directory_name_","").Replace("_aspx",".aspx").Replace("_","-");
        if (s == "default.aspx")
              { /* do something */ }



回答13:


so many answers I am using

<%if(this.MainContent.Page.Title != "mypagetitle") { %>
<%}%>

this makes it easy to exclude any single page and since your comparing a string you could even prefix pages like exclude_pagetitle and comparing a sub-string of the title. I use this commonly to exclude log in pages from certain features I don't want to load like session timeouts and live chat.




回答14:


Below code worked like a charmed ..try it

string PName = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];



回答15:


Page.Request.Url.PathAndQuery or one of the other properties of the Url Uri object should be available to you from the master page code.




回答16:


You can check the page type in the code-behind:

// Assuming MyPage1, MyPage2, and MyPage3 are the class names in your aspx.cs files:

if (this.Page is MyPage1)
{
  // do MyPage1 specific stuff
}
else if (this.Page is MyPage2)
{
  // do MyPage2 specific stuff
}
else if (this.Page is MyPage3)
{
  // do MyPage3 specific stuff
}


来源:https://stackoverflow.com/questions/269050/how-to-determine-which-child-page-is-being-displayed-from-master-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!