我创建了一个自定义基类,我的.aspx页继承自。
由于母版页继承自MasterPage而非Page,我如何创建可在我的Pages页面和母版页中使用的常用功能?
public class SitePage : System.Web.UI.Page
{
public SitePage()
{
}
public bool IsLoggedIn
{
//
}
public string HtmlTitle
{
//
}
}
答案 0 :(得分:5)
一种方法是将所有功能放在Master页面中,并始终通过Master页面调用它们。您可以在Master
中强烈输入SitePage
属性:
public class SitePage : Page
{
public new MyMaster Master { get { return base.Master as MyMaster; } }
}
然后通过Master访问值:
this.Master.IsLoggedIn
答案 1 :(得分:0)
母版页
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MyProject.master.cs"
Inherits="MyProject.MasterPages.MyProject" %>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
基页:
<%@ 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>
内容页面
<%@ 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>