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?
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.
Below code worked like a charmed ..try it
string PName = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
Request.CurrentExecutionFilePath;
or
Request.AppRelativeCurrentExecutionFilePath;
Use the Below code.
Page.ToString().Replace("ASP.","").Replace("_",".")
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
}
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"))
{
}