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

前端 未结 16 681
一生所求
一生所求 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 11:02

    This sounds like a bad idea to start with. The idea of the master is that it shouldn't care what page is there as this is all common code for each page.

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

    I use this:

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

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

    Hope that helps!

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

    Page.Request.Url.PathAndQuery or one of the other properties of the Url Uri object should be available to you from the master page code.

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

    I do something similar to this in a project of mine to dynamically attach css files based on the page being loaded. I just get the name of the file from the request:

    this.Request.Url.AbsolutePath
    

    And then extract the file name from there. I'm not sure if this will work if you are doing URL re-writes though.

    0 讨论(0)
提交回复
热议问题