Click here to Skip to main content
14,699,570 members
Articles » Web Development » ASP.NET » Howto
Posted 1 May 2009


29 bookmarked

Master Page and Content Page Interaction

Rate me:
Please Sign up or sign in to vote.
4.63/5 (5 votes)
1 May 2009CPOL
How to access Master page controls and routines from a content page and vice versa.


In a MasterPage - ContentPage structure, it is often useful to be able to access controls in the Master page from code in the Content page and vice versa. Similarly, it can also be useful to access routines in one page from the other. We use Master pages a lot, and I regularly needed to do this type of interaction, and could never find the correct code snippet, so I finally decided to write them down properly.


These interactions can be useful in a number of ways.

For example, on a Master page, you might have a Save button which saves the contents of the current Content page. However, prior to saving, you might need to validate that the data had been entered correctly. If you do this validation on the Master page and the structure of your Content pages differ, then you will end up with a large Select structure where you list all the possible Content page types and validate their data.

It is more modular to have the validation code in the Content page and call it from the Master page.

Using the code

Accessing Master page controls from a content page

Suppose there is a Label on the Master page that we want to write to from the Content page.

  1. We must include a MasterType directive at the top of the Content page ASPX file:
  2. <%@ MasterType virtualpath="~/DetailsMaster.master" %>
  3. We must include a public function in the Master page that returns a typed reference to the Label:
  4. Public Function LabelReference() As Label 
        ' Create a reference to an actual label on the page 
        LabelReference = lblDetailsItemName 
    End Function
  5. We can access the Label from the Content page using the Master syntax:
  6. Master.LabelReference.Text = "Some Text"

Accessing Master page functions from a content page

If the MasterType directive is in place in the Content page, then public functions in the Master page are available to the Content page code.

' Call the SavePageData function from the Master page 

Accessing Content page controls from a Master page

Suppose we have a Master page that contains a ContentPlaceHolder called DetailsMaster_ContentPlaceHolder. If the content page contains a TextBox called txtPass, we can declare a ContentPlaceHolder object and use it to find a reference to the TextBox.

Dim mpContentPlaceHolder As ContentPlaceHolder 
' Get a reference to the contentPlaceHolder 
mpContentPlaceHolder = _
  CType(FindControl("DetailsMaster_ContentPlaceHolder"), _
' Use FindControl to get a reference to a TextBox 
' in the ContentPage called txtPass and write to its Text property 
CType(mpContentPlaceHolder.FindControl("txtPass"), _
      TextBox).Text = "Text for the TextBox"

Accessing Content page functions from a Master page

We can use VB’s CallByName function to call a function in the Content page. This will allow us to have, e.g., the validation function for a form stored within the form and thus have different validations for each form. Suppose we have functions in a Content page like:

' function that receives a string and returns a string 
Public Function ValidateMe(ByVal strInput As String) As String 
    ValidateMe = "The input was: " + strInput 
End Function

' function that receives an Integer and returns an Integer 
Public Function SumMe(ByVal Input1 As Integer, _
       ByVal Input2 As Integer) As Integer 
    SumMe = Input1 + Input2 
End Function

Then, we can call them from the Master page by setting up wrapper functions that connect to the Content page object, and do the CallByName.

Private Function CallContentPageStringFunction(ByVal FunctionName _
                 As String, ByVal aParamArray() As String) As String 
    Dim mpContentPlaceHolder As ContentPlaceHolder 
    mpContentPlaceHolder = _
      CType(FindControl("DetailsMaster_ContentPlaceHolder"), _
    CallContentPageStringFunction = CallByName(mpContentPlaceHolder.Page, _
                                    FunctionName, vbMethod, aParamArray) 
End Function

Private Function CallContentPageIntegerFunction(ByVal FunctionName _
                 As String, ByVal aParamArray() As String) As Integer 
    Dim mpContentPlaceHolder As ContentPlaceHolder 
    mpContentPlaceHolder = _
          CType(FindControl("DetailsMaster_ContentPlaceHolder"), _
    CallContentPageIntegerFunction = CallByName(mpContentPlaceHolder.Page, _
                                     FunctionName, vbMethod, aParamArray) 
End Function

Then, we can pass parameters and get the return value as shown here:

  1. Using strings:
  2. Dim aParams(0) As String 
    aParams(0) = "Stand Up and Fight" 
    Dim strReturnValue As String = _
      CallContentPageStringFunction("ValidateMe", aParams)

    The return value in this case will be "The input was: Stand Up and Fight".

  3. Using integers:
  4. ReDim aParams(1) 
    aParams(0) = 10 
    aParams(1) = 12 
    Dim intRet As Integer = _
        CInt(CallContentPageIntegerFunction("SumMe", aParams)) 

    The return value in this case will be 220.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

John Gleeson
Web Developer Verify Technologies Ltd.
Ireland Ireland
No Biography provided

Comments and Discussions

AnswerAccess master page control from content page Pin
Programmer Dharmendra6-Jul-12 0:38
MemberProgrammer Dharmendra6-Jul-12 0:38 
Questionplzz Pin
mbsaravanan28-Feb-12 20:39
Membermbsaravanan28-Feb-12 20:39 
Questioni m not understanding Pin
Prince Antony G1-Dec-11 22:14
MemberPrince Antony G1-Dec-11 22:14 
Generalcant get it to compile. Pin
JWSantora129-Jul-09 15:57
MemberJWSantora129-Jul-09 15:57 
GeneralNot working..... Need Help Pin
Najmul Hoda24-Jun-09 22:32
MemberNajmul Hoda24-Jun-09 22:32 
Generalnice info.................. Pin
Member 28475192-Jun-09 2:07
MemberMember 28475192-Jun-09 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.