利用Page事件进行统一身份验证

 

创建一个名为BasePage的类,继承System.Web.UI.Page
 

public class BasePage:System.Web.UI.Page
    {
        public BasePage()
        {
            this.Load += new EventHandler(BasePage_Load);
        }

        void BasePage_Load(object sender, EventArgs e)
        {
            if (Session["usernum"]==null)
            {
                Response.Write("<script languge='javascript'>alert('离线时间过长');

                                window.location.href='Login.aspx'</script>");

 

              // 在复杂的框架页面中,用此法可以跳到框架最顶部,从而关闭窗口

               Response.Write("<script languge='javascript'>alert('离线时间过长');

                                top.location.href='Login.aspx'</script>");
               Response.End();
            }

        }
    }

 

其他的后台页面直接继承BasePage即可。

如:

public partial class ManagerFilesClass :BasePage
    {
        IFile newIFile = new FileService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            this.GridView1.DataSource = newIFile.ReadAllFileIntheDirectory("课程申请").ToList();
            this.GridView1.DataBind();
        }

        /// <summary>
        /// 分页
        /// </summary>
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            Bind();
        }
    }

 

 

大家看到我把Response.End()加红。为毛?

 

①请参看我的一篇博客http://blog.sina.com.cn/s/blog_67aaf4440100ms17.html

 

看到没。该页便停止执行!这样如果该页有用到session["usernum"]也不会报错了!

你可能感兴趣的:(JavaScript,框架,session,object,null,Class)