locked
Call sub from Master Page RRS feed

  • Question

  • User-1361957796 posted


    On the content page, there is let's say this:

    Public Sub MySub()


    End Sub


    On the master page I have this:

    Dim cph As ContentPlaceHolder = CType(Page.Form.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)

    Now, I want to call the Sub on the content page.

    Just can't seem to find a way to do this.

    Thanks,

    Zath

    Thursday, November 4, 2010 6:54 PM

Answers

  • User1154043639 posted

    Say you have a page called Test which will get included in Content place holder, then you have to cast page to the relevent type of call the method.

    Page

    Public Partial Class Test
    	Inherits Page
    	Protected Overrides Sub OnLoad(e As EventArgs)
    		MyBase.OnLoad(e)
    
    	End Sub
    	Public Sub MyMethod()
    
    	End Sub
    End Class
    

    Master pages  

    Public Partial Class SiteMaster
    	Inherits MasterPage
    	Protected Overrides Sub OnLoad(e As EventArgs)
    		MyBase.OnLoad(e)
    		Dim testPage As Test = TryCast(Me.Page, Test)
    		If testPage IsNot Nothing Then
    			testPage.MyMethod()
    		End If
    	End Sub
    End Class
    


    If you have to call this method in much more page implement a BasePage so that you can cast the page to base page type and then call the method


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 4, 2010 7:09 PM