ASP.NET - How do you reference a class that is not in app_code

I created a MasterPage called MyMasterPage.

public partial class MyMasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

I 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 needs to be added to make this work?

Classes are located in folders as follows:

alt text http://www.yart.com.au/stackoverflow/masterclass.png

+3
source share
3 answers

You cannot link to MyMasterPage unless you place it also in App_Code. Usually in this situation, you should create a basic master page that inherits from MasterPage. eg.

public partial class MasterPageBase : System.Web.UI.MasterPage
{
   // Declare the methods you want to call in Class1 as virtual
   public virtual void DoSomething() { }

}

- System.Web.UI.MasterPage MasterPageBase. .

public partial class MyMasterPage : MasterPageBase

Class1, ( , Master_Page, ...

public class Class1
{
    public Class1(Page Target)
    {
      MasterPageBase _m = (MasterPageBase)Target.MasterPage;
      // And I can call my overwritten methods
      _m.DoSomething();
    }
}

, , , ASP.NET.

+5

+1

fung , . App_Code , aspx-. -.

, . - -, .

, : VS 2005 Web Project System: ?

+1
source

Source: https://habr.com/ru/post/1706503/


All Articles