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

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

提交回复
热议问题