I have a public method in my content page class, I want to call this method from master page class
Thanks
views:
1591answers:
2
+1
A:
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.
Kirtan
2009-05-20 10:35:09
Excellant Idea!I already have a base class that is inherited by page, so immediate the problem is solved.
Muhammad Akhtar
2009-05-20 10:51:26
Can you plz tell me what's the difference b/w both statement or these are same(cphAdmin.Page as BasePage).yourmethod();BasePage bp = (BasePage)cphAdmin.Page;bp.yourmethod();
Muhammad Akhtar
2009-05-22 07:46:35
A:
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...);
Tolgahan Albayrak
2009-05-20 10:53:07
Reflection must be minimally used in an ASP.NET application especially in the presentation layer. And method invocation is one of the most unoptimized operation of Reflection.
Kirtan
2009-05-20 11:02:02