Check Session variable and Redirect to login page before page load

后端 未结 4 1407
旧时难觅i
旧时难觅i 2021-01-07 13:05

How can I check a variable and redirect to another page before the page loads using ASP.NET?

I\'m aware of the life cycle, and PageInit() sounds like it

4条回答
  •  萌比男神i
    2021-01-07 13:21

    I'm not aware of an onpageinit attribute. The session variable is independent of the page life cycle. Session is always available. Assuming you always use the same master page, insert your code in Pre_Init in the code behind of the Master Page.

    To do this, add the override to the code behind:

     protected override void OnPreInit(EventArgs e)
        {
            if (session.logged_in == false)
            {
              Response.Redirect("login.aspx", false);            
            }
        }
    

提交回复
热议问题