473,507 Members | 6,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Nov 30 '05 #1
9 4336
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

Nov 30 '05 #2
Why not both call a method in a public class?

"Leffe Andersson" <Leffe An*******@discussions.microsoft.com> schreef in
bericht news:79**********************************@microsof t.com...
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

Nov 30 '05 #3
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

"Remy" wrote:
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

Nov 30 '05 #4
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

"Edwin Knoppert" wrote:
Why not both call a method in a public class?

"Leffe Andersson" <Leffe An*******@discussions.microsoft.com> schreef in
bericht news:79**********************************@microsof t.com...
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


Nov 30 '05 #5
On 30 Nov 2005 05:25:49 -0800, "Remy" <rb********@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/
Nov 30 '05 #6
> 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.
Dec 1 '05 #7
See the answer from Scott Allen, that answer made it clear to me.

//Leffe

"User" wrote:
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.

Dec 1 '05 #8
Thank you Scott!

That make things clearer.

Best regards

//Leffe
"Scott Allen" wrote:
On 30 Nov 2005 05:25:49 -0800, "Remy" <rb********@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/

Dec 1 '05 #9
Thanks a bunch!
Leffe Andersson wrote:
See the answer from Scott Allen, that answer made it clear to me.

//Leffe

"User" wrote:

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.

Dec 2 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1172
by: Mark Parter | last post by:
I've "converted" the code at http://www.eggheadcafe.com/articles/20040613.asp to VB.NET as I'd like to implement this as my application uses quite complex GridViews and various other controls on...
0
1210
by: clintonG | last post by:
I've been looking for blogs, documentation, patterns, and/or your comments regarding how to design and use static methods of a "helper" class to change properties of controls in the page,...
2
3684
by: sck10 | last post by:
Hello, I am using the following to call a sub procedure in a MasterPage using vb. I am trying to find the equivalent in c#. Any help would be appreciated. Call CType(Me.Master,...
2
2521
by: ThunderMusic | last post by:
Hi, Maybe there is another way of doing this, but here's my problem... I have my web site where I have a master page to have a common layout and a common behavior on all my pages (that's what...
7
13021
by: Bon | last post by:
Dear all I create a master page with image buttons on the left-hand side for navigation. When a user clicks the student button, the content (i.e. ContentPlaceholder) in masterpage will be...
2
2362
by: Rissoman | last post by:
I have two user controls on a master page. I can easily bubble up an event on usercontrol 1 from a click event to the page.aspx, than then calls a method on the masterpage, but this causes the...
4
13024
by: faraz | last post by:
I have a javascript function that i want to call masterpage load method called. I cant add onLoad event of body or table in master page. I have put the call to script function in <form>, it...
0
1672
by: Tommy Jakobsen | last post by:
Hi. Is there a problem when trying to use Javascript on a nested masterpage? On my masterpage, i've got this in the <head>: <asp:ContentPlaceHolder ID="HeadContent" runat="server" /> And on...
4
14163
JustRun
by: JustRun | last post by:
Hi, I'm trying to develop a MultiLanguage web site using ASP.NET 2.0 with C# My problem is: I want to make my MasterPage support switching among languages, but when i put the...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.