How to call a method in a MasterPage from a class?

G

Guest

Hi folks!

I have a MasterPage with some public methods, and I would like to call them
from a (app_code) class. Is it possible to set a directive or an assembly
reference to a MasterPage from a normal class?

I have no problem to call the masterpage's methods from a contentpage, but
what I would like to do is someting like this...

public partial class MyMaster : System.Web.UI.MasterPage
{
public void MyMethod()
{
// Do some nice stuff...
}
}

public partial class MyContentPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyMaster master = (MyMaster)this.Master;
master.MyMethod(); // Works fine!

MyStaticClass.MyStaticMethod(this.Page); // This is what I would
like to do...
}
}

public static class MyStaticClass
{
public static void MyStaticMethod(Page page, bool SomeKindOfParamter)
{
// Here I would like to call the MyMaster.MyMethod() but I don't now
how??? :eek:(

// There is no problem to get a reference to the calling page's MasterPage
like this...
MasterPage master = page.Master; // Works fine, but it's ofcause useless
because MyMethod is not availible.

// This is what I would like to do...
/*
MyMaster master = (MyMaster)page.Master;
if (SomeKindOfParamter == true) // Just an example :eek:)
{
master.MyMethod(); // Is it possible to reach in some way???
// I don't now how to set a directive or an assembly reference to
MyMaster
}
*/
}

Thanks in advance!

//Leffe
 
R

Remy

Not sure if I really catched the issue, but here are my five cents:

Are the two classes in the same project? If yes, I think the only thing
you need is an using statement that includes the namespace of the
master page into your MyStaticClass.

I don't think you need assembly references, except if the two classes
are in separate projects, then you can just reference the other
project.

Let me know if that was the issue.

Cheers
Remy
 
G

Guest

Hello Remy,

Thanks for you answer, to answer your first question - yes, thay are in the
same project.

About the namespace - this was my first thought as well, but I can't find
any namespace for the masterpage :-(

Any other suggestions?

Regards

//Leffe
 
G

Guest

Hello Edwin,

Because I would like to output some data to a control that is a part of the
masterpage.
If this design works, I would have a method that can output data to the
masterpage's control from anywhere in the code.

Regards

//Leffe
 
S

Scott Allen

I don't think you need assembly references, except if the two classes
are in separate projects, then you can just reference the other
project.

ASP.NET works differently.

When a web form or master page compiles, it references the App_Code
assembly (App_Code goes into a seperate assembly).

Since we can't have a circular reference, App_Code cannot reference
the assembly of a master page - so code inside of App_Code can never
see a master page class.

The solution is to define a base class, or an interface in App_Code.
Derive the master page from the base class. The code in App_Code works
with the master page through this base class type.
 
U

User

I have no problem to call the masterpage's methods from a contentpage, but
what I would like to do is someting like this...

Please, tell me how. I can't seem to figure this out.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top