Reputation: 52241
I have a public method in my content page class, I want to call this method from master page class.
Upvotes: 0
Views: 11592
Reputation: 21
STEPS:
Add New <%@ MasterType VirtualPath="location of your masterpage" %>
directive to .aspx page
Declare one public function in MasterPage.
Call the function from content page using Master.functionName()
.
Upvotes: 2
Reputation: 3206
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...);
Upvotes: 3
Reputation: 21695
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.
Upvotes: 7