在所有頁麵共享通用行為
使用模板和主題能夠使網站所有頁麵共享設計和外觀,然而,有一些通用行為也要求共享,比如:顯示網站的統計信息,更改頁麵主題元素等等;
這裏比較好的做法是:寫一個BasePage類,讓所有的頁麵都繼承自它,而不是從標準的System.Web.UI.Page類繼承。這樣可以通過重寫該類中的On***方法來處理頁麵上的任何事件;下麵以做選擇頁麵主題為例;
1.首先寫BasePage類;
public class BasePage : System.Web.UI.Page { protected override void OnPreInit(EventArgs e) { string id = Globals.ThemesSelectorID; if (id.Length > 0) { // EVENTTARGET為主題選擇框的隱藏字段,通過它可以知道是否是由選擇主題而使頁麵會送; if (this.Request.Form["__EVENTTARGET"] == id && !string.IsNullOrEmpty(this.Request.Form[id])) { this.Theme = this.Request.Form[id]; this.Session["CurrentTheme"] = this.Theme; } else { if (this.Session["CurrentTheme"] != null) this.Theme = this.Session["CurrentTheme"].ToString(); } } base.OnPreInit(e); } }
2.由於使用到模板,所以讓模板繼承自這個類;
public partial class Template : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { } }
3.其他頁麵跟使用正常使用模板時一樣;
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="MB.TheBeerHouse.UI._Default" Title="The Beer House" MasterPageFile="~/Template.master" %>
最後更新:2017-04-02 03:42:39