阅读574 返回首页    go 阿里云 go 技术社区[云栖]


利用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()加红。为毛?

 

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

 

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

最后更新:2017-04-02 22:16:08

  上一篇:go ASP.NET中Get和Post的用法
  下一篇:go android listview仿iphone特效(下拉(下冲)或底部往上拉(上冲)有一个弹性回退)