How do I reference an ASP.net MasterPage from App_Code
Asked Answered
A

3

4

I'm working on a .net 3.5 site, standard website project.

I've written a custom page class in the sites App_Code folder (MyPage).

I also have a master page with a property.

public partial class MyMaster : System.Web.UI.MasterPage
{
...
    private string pageID = "";

    public string PageID
    {
        get { return pageID; }
        set { pageID = value; }
    }
}

I'm trying to reference this property from a property in MyPage.

public class MyPage : System.Web.UI.Page
{
...
        public string PageID
        {
            set
            {
                ((MyMaster)Master).PageID = value;
            }
            get
            {
                return ((MyMaster)Master).PageID;
            }
        }
}

I end up with "The type or namespace name 'MyMaster' could not be found. I've got it working by using FindControl() instead of a property on the MyMaster page, but IDs in the master page could change.

Abeokuta answered 30/1, 2009 at 11:42 Comment(0)
V
6

I've tended to do the following with Web Site projects:

In App_Code create the the following:

BaseMaster.cs

using System.Web.UI;

public class BaseMaster : MasterPage
{
    public string MyString { get; set; }
}

BasePage.cs:

using System;
using System.Web.UI;

public class BasePage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (null != Master && Master is BaseMaster)
        {
            ((BaseMaster)Master).MyString = "Some value";
        }
    }
}

My Master pages then inherit from BaseMaster:

using System;

public partial class Masters_MyMasterPage : BaseMaster
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(MyString))
        {
            // Do something.
        }
    }
}

And my pages inherit from BasePage:

public partial class _Default : BasePage
Vowelize answered 30/1, 2009 at 12:20 Comment(2)
Sorry to necro an old post... but this turned out to be the solution for tonight's problem. I am curious what the purpose of the MyString stuff is... doesn't seem necessary.Edraedrea
No problem @MikeFulton - The MyString property was just the example used to answer the question - Ian was trying to access a string (in his case PageID) in a common location. I guess my answer should have used his terms but I was probably just coding a generic example.Vowelize
A
1

I found some background to this, it happens because of the way things are built and referenced.

Everything in App_Code compiles into an assembly.

The rest, aspx files, code behind, masterpages etc, compile into another assemlby that references the App_Code one.

Hence the one way street.

And also why Ben's solution works. Thanks Ben.

Tis all clear to me now.

Abeokuta answered 30/1, 2009 at 13:1 Comment(0)
S
0

I realise there are already accepted solutions for this, but I just stumbled across this thread.

The simplest solution is the one listed in the Microsoft website (http://msdn.microsoft.com/en-us/library/c8y19k6h.ASPX )

Basically it says, your code will work as-is, if you include an extra directive in the child page aspx:

<%@ MasterType VirtualPath="~/MyMaster.Master" %>

Then you can directly reference the property in the base MyPage by:

  public string PageID
  {
        set
        {
            Master.PageID = value;
        }
        get
        {
            return Master.PageID;
        }
    }
Sundowner answered 13/11, 2013 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.