Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 802
jinsungy Avatar asked Sep 09 '25 15:09

jinsungy


2 Answers

I use this:

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

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

like image 155
Todd H. Avatar answered Sep 12 '25 08:09

Todd H.


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 practice 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 class name for the MasterPage. This makes to use the MasterPage in the ContentPage.

Example:

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

m.TellMasterWhoIAm(this);
like image 25
Norbert B. Avatar answered Sep 12 '25 08:09

Norbert B.