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

前端 未结 16 682
一生所求
一生所求 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:52

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

    You can use:

    Request.CurrentExecutionFilePath

    0 讨论(0)
  • 2021-02-02 10:57
    string s =   Page.ToString().Replace("ASP.directory_name_","").Replace("_aspx",".aspx").Replace("_","-");
            if (s == "default.aspx")
                  { /* do something */ }
    
    0 讨论(0)
  • 2021-02-02 10:57

    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.

    0 讨论(0)
  • 2021-02-02 11:00

    You can try this one:

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

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

    0 讨论(0)
  • 2021-02-02 11:01

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