Getting current page in Master Page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David C

    Getting current page in Master Page

    I have a Master Page that I use for several web pages. In the code-behind
    of the Master Page I have a Page_Load event that sets some information
    inside the controls of the Master Page (see example below).

    I would like to alter some of the Text properties of controls in the
    MasterPage based on the page name, e.g. Default.aspx, HomePage.aspx, etc.
    How do I determine the name of the web page that is opening? Thanks.

    David

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    LblTitle.Text = "New Web Page"
    LBtnPhotos.Text = "Initial Photos"
    End Sub


  • Mark Rae [MVP]

    #2
    Re: Getting current page in Master Page

    "David C" <dlchase@lifeti meinc.comwrote in message
    news:e$Iy8W2PJH A.5044@TK2MSFTN GP05.phx.gbl...
    How do I determine the name of the web page that is opening?
    The most important thing to remember about a MasterPage is that it is
    nothing more than a UserControl.

    The biggest mistake people make about MasterPages is to imagine that a
    MasterPage "owns" a content page when, in fact, it is the other way round.

    Therefore, if a MasterPage needs to know on which page it is being used, it
    just needs to inspect its Parent property, e.g.

    protected void Page_Init(objec t sender, EventArgs e)
    {
    switch (this.Parent.To String())
    {
    case "........" :
    {
    // do something
    break;
    }
    case "........" :
    {
    // do something
    break;
    }
    }
    }


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • Hillbilly

      #3
      Re: Getting current page in Master Page

      Study the Path class particularly Path.GetFileNam e(...)


      "David C" <dlchase@lifeti meinc.comwrote in message
      news:e$Iy8W2PJH A.5044@TK2MSFTN GP05.phx.gbl...
      >I have a Master Page that I use for several web pages. In the code-behind
      >of the Master Page I have a Page_Load event that sets some information
      >inside the controls of the Master Page (see example below).
      >
      I would like to alter some of the Text properties of controls in the
      MasterPage based on the page name, e.g. Default.aspx, HomePage.aspx, etc.
      How do I determine the name of the web page that is opening? Thanks.
      >
      David
      >
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As
      System.EventArg s) Handles Me.Load
      LblTitle.Text = "New Web Page"
      LBtnPhotos.Text = "Initial Photos"
      End Sub
      >

      Comment

      Working...