【源码】c# pagebase 用法提升

pagebase 用法提升

以往在一些需要登录的页面,总会去在每个页面去分别判断用户是否登录
或者在WEBCONFIG里面设定一些登录方式,如果没有登录就马上回滚跳转之类的方法
今天看到一个更加有趣的方法,也更加实用和灵活,就是创建一个PAGEBASE 的基类继承原始的PAGE类,然后以后的页面都继承这个PAGEBASE类,具体的验证方法都在PAGEBASE里面完成,即使以后子页面有自己独体的验证方法,也可以去重写PASEBASE类里面的验证方法,很灵活,很给力

using System;
using System.Web.UI;
namespace RonghangOAServer
{
///<summary>
/// 页面层(表示层)基类,所有页面继承该页面
///</summary>
public class PageBase : System.Web.UI.Page
{
///<summary>
/// 构造函数
///</summary>
public PageBase()
{
//this.Load+=new EventHandler(PageBase_Load);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// this.Load += new System.EventHandler(PageBase_Load); //向页面注册onload方法
this.Error += new System.EventHandler(PageBase_Error); //向页面注册onerror方法
}
//错误处理
protected void PageBase_Error(object sender, System.EventArgs e)
{
Server.Transfer("default.aspx");
}
protected virtual void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Write("这个是基类的页面初始化方法");
}
}
}
}


using System;
using System.Web;
using System.Web.UI;
using RonghangOAServer;
public partial class Default2 : PageBase
{
//protected void Page_Load(object sender, EventArgs e)
//{
// 这里会调用基类的ONLOAD方法
//}
protected override void Page_Load(object sender, EventArgs e)
{
Response.Write("这里覆盖了页面初始化方法"); //子页面重写ONLOAD方法
string s = "3";
Response.Write(s[7]);
}
}



以往在一些需要登录的页面,总会去在每个页面去分别判断用户是否登录或者在WEBCONFIG里面设定一些登录方式,如果没有登录就马上回滚跳转之类的方法今天看到一个更加有趣的方法,也更加实用和灵活,就是创建一个PAGEBASE 的基类继承原始的PAGE类,然后以后的页面都继承这个PAGEBASE类,具体的验证方法都在PAGEBASE里面完成,即使以后子页面有自己独体的验证方法,也可以去重写PASEBASE类里面的验证方法,很灵活,很给力  using System;using System.Web.UI;namespace RonghangOAServer{    /// <summary>    /// 页面层(表示层)基类,所有页面继承该页面    /// </summary>    public class PageBase : System.Web.UI.Page    {        /// <summary>        /// 构造函数        /// </summary>        public PageBase()        {            //this.Load+=new EventHandler(PageBase_Load);        }        protected override void OnInit(EventArgs e)        {            base.OnInit(e);            //    this.Load += new System.EventHandler(PageBase_Load);    //向页面注册onload方法            this.Error += new System.EventHandler(PageBase_Error);  //向页面注册onerror方法        }        //错误处理        protected void PageBase_Error(object sender, System.EventArgs e)        {            Server.Transfer("default.aspx");        }        protected virtual void Page_Load(object sender, EventArgs e)        {            if (!Page.IsPostBack)            {                Response.Write("这个是基类的页面初始化方法");            }        }    }}  using System;using System.Web;using System.Web.UI;using RonghangOAServer;public partial class Default2 : PageBase{    //protected void Page_Load(object sender, EventArgs e)    //{    //      这里会调用基类的ONLOAD方法    //}    protected override void Page_Load(object sender, EventArgs e)    {        Response.Write("这里覆盖了页面初始化方法"); //子页面重写ONLOAD方法        string s = "3";        Response.Write(s[7]);    }}

原文地址:https://www.cnblogs.com/chusiping/p/2266462.html