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

前端 未结 16 714
一生所求
一生所求 2021-02-02 10: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?

相关标签:
16条回答
  • 2021-02-02 10:43

    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.

    0 讨论(0)
  • 2021-02-02 10:46

    Below code worked like a charmed ..try it

    string PName = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
    
    0 讨论(0)
  • 2021-02-02 10:47
    Request.CurrentExecutionFilePath;
    

    or

    Request.AppRelativeCurrentExecutionFilePath;
    
    0 讨论(0)
  • 2021-02-02 10:50

    Use the Below code.

    Page.ToString().Replace("ASP.","").Replace("_",".")
    
    0 讨论(0)
  • 2021-02-02 10:51

    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
    }
    
    0 讨论(0)
  • 2021-02-02 10:51

    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"))
    {
    
    }
    
    0 讨论(0)
提交回复
热议问题