But many people like to
protected void Page_Load (object sender, EventArgs e) {}
Inside to write code, even in some buttons to write to determine whether the session exists ~ ~
This certainly can achieve the effect, the problem is, if there are 1000 pages ~ ~ You need to CTRL + C ... Ctrl + V very many times ~ ~
My idea is to write a basepage class inheritance System.Web.UI.Page
The public class BasePage:System.Web.UI.Page {//pageunload event does not mean that the browser is closed, but the page is closed, so the following event is still executed when the refresh occurs protected void Page_ Unload (object sender, EventArgs e) {} protected override void Onpreinit (EventArgs e) {base. Onpreinit (e); if (! Sessiondata.islogin ()) {//write here to jump to landing page: for example: Response.Redirect (String. Format ("~/relogin.aspx? Page={0} ", Request.path)); }}
Why do I have to bring the page parameter here, in order to return to the page before login after successful login
In addition I also contribute a Sessiondata class:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Web; Using Expressplatform.common; Namespace ExpressPlatform.Web.AppCode {public class SessionKey {Public Const string UserInfo = "user";}//<summary >////data in all sessions, in this category management///</summary> public class Sessiondata {///<summary>///For user information in session// </summary>//<returns></returns> public static Mdlsessioncustomerinfo GetUserInfo () { Mdlsessioncustomerinfo userInfo = Sessionmanager<mdlsessioncustomerinfo>. Getsessionobject (Sessionkey.userinfo); if (UserInfo = = null) {UserInfo = new mdlsessioncustomerinfo ();//Store the content in the application sessionmanager<mdlsessioncustomerinfo& gt;. Setsessionobject (Sessionkey.userinfo, UserInfo); } return userInfo; }///<summary>//Reset user information in session///</summary>//<param name= "UserInfo" ></param> public s tatic void Setuserinfo (Mdlsessioncustomerinfo userInfo) {sessionmanager<mdlsessioncustomerinfo>.Setsessionobject (Sessionkey.userinfo, UserInfo); }///<summary>///Clear Session User Information///</summary> public static void Clearuserinfo () {Sessionmanager<mdlse Ssioncustomerinfo>. Setsessionobject (sessionkey.userinfo, NULL); }///<summary>//whether login///</summary>//<returns></returns> public static bool IsLogin () {Boo l RET = false; Mdlsessioncustomerinfo userInfo = Sessionmanager<mdlsessioncustomerinfo>. Getsessionobject (Sessionkey.userinfo); if (userInfo! = null) ret = TRUE; return ret; } } }
public class BasePage:System.Web.UI.Page