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?
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With