Asp.net PageBase继承
来源:互联网 发布:系统还原后数据恢复 编辑:程序博客网 时间:2024/05/21 22:44
Page_Load方法响应了Load事件,这个事件是在System.Web.WebControl.Control类中定义的(这个类是Page和所有服务器控件的祖宗),并且在OnLoad方法中被触发。
很多人可能碰到过这样的事情,写了一个PageBase类,然后在Page_Load中来验证用户信息,结果发现不管验证是否成功,子类页面的Page_Load总是会先执行,这个时候很可能留下一些安全性的隐患,用户可能在没有得到验证的情况下就执行了子类中的Page_Load方法。
出现这个问题的原因很简单,因为Page_Load方法是在OnInit中被添加到Load事件中的,而子类的OnInit方法中是先添加了Load事件,然后再调用base.OnInit,这样就造成了子类的Page_Load被先添加,那么先执行了。
要解决这个问题也很简单,有两种方法:
1) 在PageBase中重载OnLoad方法,然后在OnLoad中验证用户,然后调用base.OnLoad,因为Load事件是在OnLoad中触发,这样我们就可以保证在触发Load事件之前验证用户。
2) 在子类的OnInit方法中先调用base.OnInit,这样来保证父类先执行Page_Load
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
/// <summary>
///first write by
///up by ahuinan
/// </summary>
public class PageBase:System.Web.UI.Page
{
public PageBase()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(PageBase_Load);
this.Error += new System.EventHandler(PageBase_Error);
}
//错误处理
protected void PageBase_Error(object sender, System.EventArgs e)
{
string errMsg = string.Empty;
Exception currentError = HttpContext.Current.Server.GetLastError();
errMsg += "<h1>系统错误:</h1><hr/>系统发生错误, " +
"该信息已被系统记录,请稍后重试或与管理员联系。<br/>" +
"错误地址: " + Request.Url.ToString() + "<br/>" +
"错误信息: " + currentError.Message.ToString() + "<hr/>" +
"<b>Stack Trace:</b><br/>" + currentError.ToString();
HttpContext.Current.Response.Write(errMsg);
Server.ClearError();
}
private void PageBase_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (HttpContext.Current.Session["username"] != null)
{
HttpContext.Current.Response.Write("登陆测试");
}
else
{
HttpContext.Current.Response.Write("你不是阿会楠,不要登陆");
}
}
}
}
- Asp.net PageBase继承
- Asp.net PageBase继承
- ASP.NET 2.0 技巧:用MasterPage 代替 PageBase
- ASP.NET 常用方法类 - PageBase 页面基类
- PAGEBASE
- Asp.net 页面 继承
- asp.net 继承身份验证
- ASP.NET 页面继承
- ASP.NET 页面继承
- Asp.Net继承实现身份验证
- Asp.Net继承实现身份验证
- ASP.NET 配置文件层次结构和继承
- PageBase.cs
- 利用继承PageBase来判断登陆状态,没有登陆,则跳转到登陆页面
- Asp.net中对像使用继承的方法(:)
- asp.net 用继承方法实现页面判断session
- Asp.Net 继承System.Web.UI.Page实现权限检测
- ASP.NET继承类实现登陆及Session验证
- QQ腾讯登录相关的HTTP协议
- sigh
- C#委托,事件理解入门
- T-Sql语句删除全部存储过程
- iphone游戏之旅第四周
- Asp.net PageBase继承
- 《海棠》-苏轼
- linux make 基础: build host target 与交叉编译
- Hibemate基本概念和CURD详解
- 关于EDA自动售货机的设计
- HQL
- 用Visual Studio 2008成功编译GMSH 2.5
- 从面向对象设计谈c#中接口和委托的区别运用
- 树状数组和线段树