Search code examples
c#asp.net.net-3.5master-pages

Access Master Page class in C# .Net 3.5


So... I'm having to get someone's old (2010) .Net 3.5 C# code working... and of course the dev didn't have the project files anymore just the source...

Long story is code is trying to access a property of the master page class and this looks like it should be working according to everything I can find but it's not.

Example of the MasterPage code behind:

public partial class myMasterPage : System.Web.UI.MasterPage
{
   private string pageName = "";
   public string PageName
   {
       get { return PageName; }
       set { pageName = value; }
   }

   private int pageID;
   public int PageID
   {
       get { return pageID; }
       set { pageID = value; }
   }
}

One of the pages code:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" Inherits="Help" Codebehind="Help.aspx.cs" %>
<%@ MasterType TypeName="ISLMasterPage"  %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

And finally the code behind for that page:

public partial class Help : BasePage
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        base.PageID = 1; 
        Master.PageID = 1;  
    }
}

I've tried changing the MasterType to virtual path... nothing... error I'm getting is:

E'System.Web.UI.MasterPage' does not contain a definition for 'PageID' and no extension method 'PageID' accepting a first argument of type 'System.Web.UI.MasterPage' could be found (are you missing a using directive or an assembly reference?)

So to me it's obvious that it's not picking up the fact that we want the code behind.

I'm guessing this is something obvious but I've never organized my code in such a way.


Solution

  • Your code doesn't know that your master page is a "myMasterPage"

    protected void Page_PreInit(object sender, EventArgs e)
    {
        base.PageID = 1; 
        (Master as myMasterPage).PageID = 1;  
    }