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
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
"Leffe Andersson" <Leffe Ande...@discussions.microsoft.com> schreef in
bericht news:79749445-B08F-4307...@microsoft.com...
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
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
>
>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.
Please, tell me how. I can't seem to figure this out.
Thanks.
//Leffe
That make things clearer.
Best regards
//Leffe