I have created a MasterPage called MyMasterPage.
public partial class MyMasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
I have also created a class called Class1 in app_code:
public class Class1
{
public Class1()
{
MyMasterPage m;
}
}
In Class1 I would like to reference MyMasterPage but I get a compiler warning:
The type or namespace name 'MyMasterPage' could not be found (are you missing a using directive or an assembly reference?)
What code do I need to add to get this to work?
The classes are in folders as follows:
alt text http://www.yart.com.au/stackoverflow/masterclass.png
You won't be able to reference MyMasterPage unless you put it under App_Code as well. Normally in such a situation you would create a base master page that inherits from MasterPage. e.g.
public partial class MasterPageBase : System.Web.UI.MasterPage
{
// Declare the methods you want to call in Class1 as virtual
public virtual void DoSomething() { }
}
Then in your actual master pages, instead of inheriting from System.Web.UI.MasterPage,inherit from your MasterPageBase. Overwrite the virtual methods in your inheriting pages.
public partial class MyMasterPage : MasterPageBase
In Class1 where you need to refer to it (and I'm assuming you get the master page from a Page class' MasterPage property, your code will look like...
public class Class1
{
public Class1(Page Target)
{
MasterPageBase _m = (MasterPageBase)Target.MasterPage;
// And I can call my overwritten methods
_m.DoSomething();
}
}
It's quite a long winded way but so far the only thing that I can think of that works given the ASP.NET model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With