閱讀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特效(下拉(下衝)或底部往上拉(上衝)有一個彈性回退)