• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

asp.net 继承自Page实现统一页面验证与错误处理

asp 搞代码 4年前 (2022-01-03) 10次浏览 已收录 0个评论

一直以来,我都在思考以前一个项目中,后台文件中很多的.aspx文件上的权限判断问题,傻乎乎的我基本上每个文件当时都给加了一句

代码如下:
isAdmin();

因为当时没有用母版页去做,所以不能在母版页中统一判断权限,而当时我限于自己水平,也没有采用继承自Page这个类的方法去统一处理一些页面加载的时候都要处理的事情。现在根据“李天平(动软)”的一些代码记录下,也希望大家要学会使用继承啊!
看下一个简单的继承自Page的PageBase:

代码如下:
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;

///

///first write by 李天平
///up by ahuinan 2009-4-18
///

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 curren来源gaodaimacom搞#代%码网tError = HttpContext.Current.Server.GetLastError();
errMsg += “

系统错误:


系统发生错误, ” +
“该信息已被系统记录,请稍后重试或与管理员联系。
” +
“错误地址: ” + Request.Url.ToString() + “
” +
“错误信息: ” + currentError.Message.ToString() + “


” +
Stack Trace:
” + 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(“搜索吧sosuo8.com登陆测试”);
}
else
{
HttpContext.Current.Response.Write(“你不是阿会楠,不要登陆”);
}
}
}
}

使用的时候:

代码如下:
public partial class _Default :PageBase
{

protected void Page_Load(object sender, EventArgs e)
{
int ID = int.Parse(Request.QueryString[“ID”]);
Response.Write(“id:”+ID.ToString());
}
}

以上就是asp.net 继承自Page实现统一页面验证与错误处理的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:asp.net 继承自Page实现统一页面验证与错误处理

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址