473,772 Members | 2,414 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.M asterPage
{
public void MyMethod()
{
// Do some nice stuff...
}
}

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

MyStaticClass.M yStaticMethod(t his.Page); // This is what I would
like to do...
}
}

public static class MyStaticClass
{
public static void MyStaticMethod( Page page, bool SomeKindOfParam ter)
{
// Here I would like to call the MyMaster.MyMeth od() 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 (SomeKindOfPara mter == 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 4350
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*******@discu ssions.microsof t.com> schreef in
bericht news:79******** *************** ***********@mic rosoft.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.M asterPage
{
public void MyMethod()
{
// Do some nice stuff...
}
}

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

MyStaticClass.M yStaticMethod(t his.Page); // This is what I would
like to do...
}
}

public static class MyStaticClass
{
public static void MyStaticMethod( Page page, bool SomeKindOfParam ter)
{
// Here I would like to call the MyMaster.MyMeth od() 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 (SomeKindOfPara mter == 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*******@discu ssions.microsof t.com> schreef in
bericht news:79******** *************** ***********@mic rosoft.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.M asterPage
{
public void MyMethod()
{
// Do some nice stuff...
}
}

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

MyStaticClass.M yStaticMethod(t his.Page); // This is what I would
like to do...
}
}

public static class MyStaticClass
{
public static void MyStaticMethod( Page page, bool SomeKindOfParam ter)
{
// Here I would like to call the MyMaster.MyMeth od() 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 (SomeKindOfPara mter == 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********@hot mail.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********@hot mail.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
1191
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 some pages (viewstate alone was 110Kb+ on some pages!!). Anyway, I've setup the classes required and created a test page which inherits from the new BasePage class. It works well. How can I now use this in an environment that uses mostly Master...
0
1221
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, especially controls such as multiple LinkButtons and Panels which may be located in a MasterPage or loaded into the Master using a Content Page. Noting I've tried public properties, FindControl and what not with no results. Here's what I've got, I'm...
2
3691
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, mpColumn02).LoadMenu("menu_svcprtf") Thanks, sck10
2
2534
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 it's intended for)... But now here comes the time where some pages need some processing (like, is the user logged? and those kind of things)... If it was only on 1 or 2 pages, I'd says, i'll put it directly in the code and case closed, but there...
7
13046
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 changed from default.aspx to students.aspx. I tried to create a subroutine in MasterPage.master.vb to do it. The code is as following. But, the default.aspx's content is shown in MasterPage even I clicked the student button.
2
2375
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 screen to do a postbacka nd full load. What I'm doing is i have a button on one user control that when clicked it will dynamically load a usercontrol into a placeholder on the masterpage. I know that if I were to put everything into an update...
4
13034
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 works on every pageLoad. seems my problem is solved. But when the pages uses UpdatePanel and it gets asynchronously postback. the function call at master page doesnt execute????? thought master page Load event gets called even when pages are in...
0
1689
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 the nested masterpage: <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> <asp:ContentPlaceHolder ID="NestedHeadContent" runat="server" />
4
14182
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 "InitializeCulture()" inside the masterpage.cs, I got this erreor: MasterPage.InitializeCulture()': no suitable method found to override Note the following code works fine with xxx.aspx page, but not with MasterPage Here is my code: public override void...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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 we have to send another system
2
3609
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.