Tuesday, May 11, 2010

How to access public members and controls of master page in a child (content) page and Accessing Content page functions from a Master page


ShareThis

How to: Reference ASP.NET Master Page Content

You can write code in content pages that references properties, methods, and controls in the master page, with some restrictions. The rule for properties and methods is that you can reference them if they are declared as public members of the master page. This includes public properties and public methods. You can reference controls on the master page independently of referencing public members.

To reference a public member on the master page
  1. Add a @ MasterType directive in the content page. In the directive, set the VirtualPath attribute to the location of the master page, as in this example:

    <%@ MasterType virtualpath="~/Masters/Master1.master" %>


    This directive causes the content page's Master property to be strongly typed.





  2. Write code that uses the master page's public member as a member of the Master property, as in this example, which assigns the value of a public property named CompanyName from the master page to a text box on the content page:



    Visual Basic



    CompanyName.Text = Master.CompanyName


    C#



    CompanyName.Text = Master.CompanyName;




To reference a control on the master page




  • Use the FindControl method, using the value returned by the Master property as the naming container.



    The following code example shows how to use the FindControl method to get a reference to two controls on the master page, a TextBox control and a Label control. Because the TextBox control is inside a ContentPlaceHolder control, you must first get a reference to the ContentPlaceHolder and then use its FindControl method to locate the TextBox control.



    Visual Basic



    Sub Page_Load()
    Dim mpContentPlaceHolder As ContentPlaceHolder
    Dim mpTextBox As TextBox
    mpContentPlaceHolder = _
    CType(Master.FindControl("ContentPlaceHolder1"), _
    ContentPlaceHolder)
    If Not mpContentPlaceHolder Is Nothing Then
    mpTextBox = CType(mpContentPlaceHolder. _
    FindControl("TextBox1"), TextBox)
    If Not mpTextBox Is Nothing Then
    mpTextBox.Text = "TextBox found!"
    End If
    End If

    ' Gets a reference to a Label control not in a
    ' ContentPlaceHolder
    Dim mpLabel As Label
    mpLabel = CType(Master.FindControl("masterPageLabel"), Label)
    If Not mpLabel Is Nothing Then
    Label1.Text = "Master page label = " + mpLabel.Text
    End If
    End Sub


    C#



    void Page_Load()
    {
    // Gets a reference to a TextBox control inside
    // a ContentPlaceHolder
    ContentPlaceHolder mpContentPlaceHolder;
    TextBox mpTextBox;
    mpContentPlaceHolder =
    (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
    if(mpContentPlaceHolder != null)
    {
    mpTextBox =
    (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
    if(mpTextBox != null)
    {
    mpTextBox.Text = "TextBox found!";
    }
    }

    // Gets a reference to a Label control that not in
    // a ContentPlaceHolder
    Label mpLabel = (Label) Master.FindControl("masterPageLabel");
    if(mpLabel != null)
    {
    Label1.Text = "Master page label = " + mpLabel.Text;
    }
    }


  • =====================================================


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:



Collapse



' 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.



Collapse



Private Function CallContentPageStringFunction(ByVal FunctionName _
As String, ByVal aParamArray() As String) As String
Dim mpContentPlaceHolder As ContentPlaceHolder
mpContentPlaceHolder = _
CType(FindControl("DetailsMaster_ContentPlaceHolder"), _
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"), _
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:

    Collapse



    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".




  2. Using integers:

    Collapse



    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.





References:



(MSDN) http://msdn.microsoft.com/en-us/library/xxwa0ff0(VS.90).aspx



(CodeProject) http://www.codeproject.com/KB/aspnet/Master_and_Contents.aspx


0 comments: