loyalflow
loyalflow

Reputation: 14879

Advice on how to create a common base class (or functionality) between a page and master?

I created a custom base class that my .aspx pages inherit from.

Since Master page's inherit from MasterPage and not Page, how can I create common functionality that are available in both my Pages and Master pages?

public class SitePage : System.Web.UI.Page
{

  public SitePage()
  {

  }

  public bool IsLoggedIn
  {
         //
  }

  public string HtmlTitle
  {
           //
   }
}

Upvotes: 1

Views: 549

Answers (2)

IrishChieftain
IrishChieftain

Reputation: 15253

Master Page:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MyProject.master.cs"
    Inherits="MyProject.MasterPages.MyProject" %>

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">   
</asp:ContentPlaceHolder>

Base page:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/MyProject.Master"
    AutoEventWireup="true" CodeBehind="BasePage.aspx.cs"
        Inherits="MyProject.BasePage" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    runat="server">
</asp:Content>

Content Page:

<%@ Page Title="MyProject - Home" Language="C#"
    MasterPageFile="~/MasterPages/MyProject.Master" AutoEventWireup="true"
        CodeFileBaseClass="MyProject.BasePage" CodeFile="Default.aspx.cs"
            Inherits="MyProject.Default"
                Meta_Description="Code Snippet: Master Page and Base Page"
                    Meta_Keywords="master, base, content" Theme="Style" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
   runat="server">
</asp:Content>

Upvotes: 0

gilly3
gilly3

Reputation: 91497

One approach would be to put all the functionality in the Master page and always call them by going through the Master page. You can strongly type the Master property in SitePage:

public class SitePage : Page
{
    public new MyMaster Master { get { return base.Master as MyMaster; } }
}

Then access the values through the Master:

this.Master.IsLoggedIn

Upvotes: 5

Related Questions