Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

15 views
Skip to first unread message

Andersson@discussions.microsoft.com Leffe Andersson

unread,
Nov 30, 2005, 7:55:03 AM11/30/05
to
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??? :o(

// 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 :o)
{
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

Remy

unread,
Nov 30, 2005, 8:25:49 AM11/30/05
to
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

Edwin Knoppert

unread,
Nov 30, 2005, 8:45:35 AM11/30/05
to
Why not both call a method in a public class?

"Leffe Andersson" <Leffe Ande...@discussions.microsoft.com> schreef in
bericht news:79749445-B08F-4307...@microsoft.com...

Leffe Andersson

unread,
Nov 30, 2005, 11:10:09 AM11/30/05
to
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

Leffe Andersson

unread,
Nov 30, 2005, 11:18:15 AM11/30/05
to
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

Scott Allen

unread,
Nov 30, 2005, 2:20:18 PM11/30/05
to
On 30 Nov 2005 05:25:49 -0800, "Remy" <rblae...@hotmail.com> wrote:

>
>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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

User

unread,
Nov 30, 2005, 7:12:01 PM11/30/05
to Leffe Andersson
> 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.

Leffe Andersson

unread,
Dec 1, 2005, 4:29:02 AM12/1/05
to
See the answer from Scott Allen, that answer made it clear to me.

//Leffe

Leffe Andersson

unread,
Dec 1, 2005, 4:47:02 AM12/1/05
to
Thank you Scott!

That make things clearer.

Best regards

//Leffe

User

unread,
Dec 1, 2005, 7:15:56 PM12/1/05
to Leffe Andersson
Thanks a bunch!
0 new messages