asp.net 繼承自Page實現統一頁面驗證與錯誤處理

來源:互聯網
上載者:User

複製代碼 代碼如下: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;

/// <summary>
///first write by 李天平
///up by ahuinan 2009-4-18
/// </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("搜尋吧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());
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.