Search code examples
asp.netmethodsmaster-pages

Content page class method calling from master page class


I have a public method in my content page class, I want to call this method from master page class.


Solution

  • You can inherit your page from a base class. Then you can create a virtual method in your base class which will get overridden in your page. You can then call that virtual method from the master page like this -

    (cphPage.Page as PageBase).YourMethod();
    

    Here, cphPage is the ID of the ContentPlaceHolder in your master page. PageBase is the base class containing the YourMethod method.

    EDIT: Of course, you'll have to put a null checking before you call the YourMethod method using the page's instance.