A master page can share data with content pages in ASP.Net. Content pages can also send data UPWARDS to the master pages. Doing this can be a bit tricky as there are good and bad ways to achieve the functionality. The rule to remember is do NOT make things public, instead please make data available with properties.

Here is the idea in brief:

  1. Make a master page
  2. Make a content page inheriting the master from 1
  3. On the content page create a MasterType reference like this
    <%@ Page Language=”VB” MasterPageFile=”~/TheMaster.master” …
    <%@ MasterType VirtualPath=”~/TheMaster.master” %>
  4. MasterType allows access to the master page as though it were an object
    We don’t want to get me started on why this is not the default behaviour.
  5. Make a property to expose the control upon the master page like this
    Public Property TextBoxText() As String
    Get
    Return aTextBoxOnTheMasterPage.Text
    End Get
    Set(ByVal value As String)
    aTextBoxOnTheMasterPage.Text = value
    End Set
    End Property
  6. From the content page, you may now change the values in that box or read them through the property you created using code like this:
    Master.TextBoxText = “THE NEW TEXT!”
  7. Notice the TextBoxText after the Master. and see that it is the same as the name of your created property. This is the proper way to access things page to page. Do not make textbox and the like public as this would violate OOP programming rules!

Scott Mitchell wrote a tutorial on this topic over at 4GuysFromRolla.

Also, Scott has a great general tutorial on master pages over at asp.net.

Tags: , , , , , , , ,