[ACCEPTED]-How to determine which Child Page is being displayed from Master Page?-master-pages

Accepted answer
Score: 31

I use this:

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

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

Hope that 1 helps!

Score: 24

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

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


MyMaster m = (MyMaster)this.Master;


Hope 1 this helps.

Score: 9

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

Score: 9

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

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

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

this 1 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");
Score: 7

Use the Below code.



Score: 4
Score: 3

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

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

or a bit more sophisticated, but less 1 readable:

if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/YourPageName.aspx", StringComparison.OrdinalIgnoreCase))
   // your code here
Score: 1




Score: 0

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


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

Score: 0

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

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

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

Score: 0

You can try this one:

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

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

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


Score: 0

so many answers I am using

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

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

Score: 0

Below code worked like a charmed ..try it 1

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

More Related questions