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

Thanks

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

if you do not want to use any base page

add this to your master page,

private object callContentFunction(string methodName, params object[] parameters)
{
    Type contentType = this.Page.GetType();
    System.Reflection.MethodInfo mi = contentType.GetMethod(methodName);
    if(mi == null)return null;
    return mi.Invoke(this.Page, parameters);
}

then use it

callContentFunction(“myPublicMethodName”, myParam1, myParam2…);

Method 3

STEPS:

  1. Add New <%@ MasterType VirtualPath="location of your masterpage" %> directive to .aspx page
  2. Declare one public function in MasterPage.
  3. Call the function from content page using Master.functionName().


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x