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

16

26

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?

Mascia answered 6/11, 2008 at 15:15 Comment(0)
R
9

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.

Rathenau answered 6/11, 2008 at 15:37 Comment(7)
+1 Exactly. This very much goes against what a master page is about.Ingroup
This is a good general rule, but how about something like a breadcrumb? You want it on every page, but you need to know what specific page is loaded to determine the crumbs. For this I typically expose a public function on the masterpage for setting the final crumb, this is called by the page. The other crumbs are determined by the sitemap.Brozak
What would your recommendation be if your master page contained logic to redirect to a 'safe' page under certain circumstances UNLESS the safe page was being displayed?Giorgia
@norbertB gives a pretty good solution IMO on how to accomplish the desired outcome without having the master page needing to know what page is being displayed.Anh
So, what if I wanted an image slider only on my landing page, and no-where else? (and my ContentPlaceHolder is wrapped in a div with a generic style, which the image slider shouldn't have)Velez
I find the best answers to "how" questions are always "no"Mcwhorter
This is a super old answer, with new Meta tags handling all social media content, this is a good idea to have default meta tags on the master page and the page specific tags on each other page if necessaryPelias
G
32

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!

Guesstimate answered 9/7, 2010 at 18:14 Comment(1)
You can use string pageName = this.ContentPlaceHolder1.Page.GetType().BaseType.Name; This returns base class type of child page. For most cases it gives the Page Adress without "aspx" tag. For example "Default".Mirtamirth
M
24

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.

Melar answered 6/11, 2008 at 16:29 Comment(3)
I like this. Sometimes the master page does need a little bit of information from its users. This is a clean way to do it.Primm
This is a good solution to the question. What the original poster was asking how to do wasn't really a good practice to follow, and you gave an solution that directed the OP to following a better practice. Thanks @norbertB!Anh
Here's a reminder for myself and other forgetful souls. For Norbert's answer to work, be sure to add @ MasterType directives to the child Web Forms' .aspx files so that the C# code can access the master's properties and methods like Master.SomeProperty = "SomeValue";Petunia
R
9

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.

Rathenau answered 6/11, 2008 at 15:37 Comment(7)
+1 Exactly. This very much goes against what a master page is about.Ingroup
This is a good general rule, but how about something like a breadcrumb? You want it on every page, but you need to know what specific page is loaded to determine the crumbs. For this I typically expose a public function on the masterpage for setting the final crumb, this is called by the page. The other crumbs are determined by the sitemap.Brozak
What would your recommendation be if your master page contained logic to redirect to a 'safe' page under certain circumstances UNLESS the safe page was being displayed?Giorgia
@norbertB gives a pretty good solution IMO on how to accomplish the desired outcome without having the master page needing to know what page is being displayed.Anh
So, what if I wanted an image slider only on my landing page, and no-where else? (and my ContentPlaceHolder is wrapped in a div with a generic style, which the image slider shouldn't have)Velez
I find the best answers to "how" questions are always "no"Mcwhorter
This is a super old answer, with new Meta tags handling all social media content, this is a good idea to have default meta tags on the master page and the page specific tags on each other page if necessaryPelias
R
9

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");
                }
Relly answered 23/8, 2010 at 12:33 Comment(1)
I am using VB and this works for me: If ContentPlaceHolder1.Page.GetType().BaseType Is GetType(Dashboard)Sniff
C
7

Use the Below code.

Page.ToString().Replace("ASP.","").Replace("_",".")
Cartulary answered 8/7, 2013 at 11:50 Comment(2)
Thanks so much. its worked. İt means, we can reach to any page from masterpage...Furor
this was my solution, i needed the name of the content page to evaluate a funcion that should or shouldn't show a labelCanst
H
4

You can use:

Request.CurrentExecutionFilePath

Helfand answered 6/11, 2008 at 15:20 Comment(1)
This did the job in my case as I was asking for the precise file name, not the object.Laddy
D
3

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
}
Discalced answered 30/7, 2015 at 7:45 Comment(0)
R
1
Request.CurrentExecutionFilePath;

or

Request.AppRelativeCurrentExecutionFilePath;
Rectifier answered 25/3, 2014 at 8:28 Comment(0)
B
0

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.

Brozak answered 6/11, 2008 at 15:23 Comment(0)
A
0

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"))
{

}
Acuna answered 27/3, 2013 at 10:28 Comment(0)
P
0

You can try this one:

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

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

Province answered 14/10, 2013 at 9:56 Comment(0)
P
0
string s =   Page.ToString().Replace("ASP.directory_name_","").Replace("_aspx",".aspx").Replace("_","-");
        if (s == "default.aspx")
              { /* do something */ }
Podite answered 11/5, 2015 at 13:18 Comment(0)
P
0

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.

Portauprince answered 20/4, 2016 at 12:41 Comment(0)
U
0

Below code worked like a charmed ..try it

string PName = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
Unstressed answered 24/11, 2017 at 5:39 Comment(0)
H
-2

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.

Helping answered 6/11, 2008 at 15:22 Comment(0)
I
-4

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
}
Ignore answered 6/11, 2008 at 15:40 Comment(1)
This is what I'd expect too, but this doesn't work. ASP.NET seems to mangle the class declarations (annoyingly!) so while it looks like you're declaring MyPage1, you're actually not. You get something like ASP.MyPage1_aspx, and you can't refer to this type at compile time.Codeine

© 2022 - 2024 — McMap. All rights reserved.