代码改变世界

一个重写Page基类的例子

2008-05-22 11:11  Vincent.C  阅读(235)  评论(0编辑  收藏  举报

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// BasePage 的摘要说明
/// </summary>
public class BasePage : Page
{
    //重写Page OnPreInit事件
    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);

        //如果是登陆页面,取消登陆验证
        if (GetRequestFilename().Equals("Mes_Login.aspx"))
        {
            goto Finish;
        }

        //开始页面登陆验证
        bool isLog = false;
        try
        {
            isLog = Session["LOGIN_FLAG"].ToString().Equals("1");
        }
        catch (Exception ex) { }

        if (!isLog)
        {
            Alert("您还未登录");
            Response.Redirect("~/Mes_Login.aspx?ReturnUrl=" + Request.RawUrl);
        }

    Finish:
        { }

    }

    /// <summary>
    /// 在页面弹出提示框(集成于基类BasePage)
    /// </summary>
    /// <param name="msg">在提示框中显示的内容</param>
    public void Alert(string msg)
    {
        this.RegisterClientScriptBlock("js", "<script>alert('" + msg + "')</script>");
    }
    /// <summary>
    /// 在页面弹出提示框(静态型)
    /// </summary>
    /// <param name="msg">在提示框中显示的内容</param>
    public static void Alert(Page pg, string msg)
    {
        pg.RegisterClientScriptBlock("js", "<script>alert('" + msg + "')</script>");
    }

    /// <summary>
    /// 取得当前Request Url的文件名称
    /// </summary>
    /// <returns>返回当前Request Url的文件名称</returns>
    public string GetRequestFilename()
    {


        return System.IO.Path.GetFileName(Request.Path);
    }

}