但是很多人都喜歡在
復(fù)制代碼代碼如下:
protected void Page_Load(object sender, EventArgs e)
{}
里面來(lái)寫代碼,甚至在某些按鈕里面寫判斷session是否存在~~
這樣當(dāng)然是能實(shí)現(xiàn)效果的,問(wèn)題就在,如果有1000個(gè)頁(yè)面~~你需ctrl+C。。。Ctrl+V 很多次~~~
我的思路就是寫一個(gè)BasePage類繼承 System.Web.UI.Page
復(fù)制代碼代碼如下:
public class BasePage : System.Web.UI.Page
{
//pageunload事件,并不是指瀏覽器關(guān)閉,而是指頁(yè)面關(guān)閉,所以刷新的時(shí)候,依然會(huì)執(zhí)行以下事件
protected void Page_Unload(object sender, EventArgs e)
{
}
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (!SessionData.IsLogin())
{//這里寫 跳轉(zhuǎn)到登陸頁(yè)面:例如:
Response.Redirect(string.Format("~/ReLogin.aspx?Page={0}", Request.Path));
}}
為什么我這里要帶 Page 參數(shù),就是為了在登錄成功以后可以返回到登錄前的那一個(gè)頁(yè)面
另外我也貢獻(xiàn)一個(gè)SessionData類:
復(fù)制代碼代碼如下:
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>
/// 所有session中的數(shù)據(jù),在該類管理
/// </summary>
public class SessionData
{
/// <summary>
/// 獲取session 中的 用戶信息
/// </summary>
/// <returns></returns>
public static MdlSessionCustomerInfo GetUserInfo()
{
MdlSessionCustomerInfo userInfo = SessionManager<MdlSessionCustomerInfo>.GetSessionObject(SessionKey.UserInfo);
if (userInfo == null)
{
userInfo = new MdlSessionCustomerInfo();
//把內(nèi)容儲(chǔ)存到應(yīng)用程序
SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, userInfo);
}
return userInfo;
}
/// <summary>
/// 重新設(shè)置session 中的用戶信息
/// </summary>
/// <param name="userInfo"></param>
public static void SetUserInfo(MdlSessionCustomerInfo userInfo)
{
SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, userInfo);
}
/// <summary>
/// 清楚session中用戶信息
/// </summary>
public static void ClearUserInfo()
{
SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, null);
}
/// <summary>
/// 是否登入
/// </summary>
/// <returns></returns>
public static bool IsLogin()
{
bool ret = false;
MdlSessionCustomerInfo userInfo = SessionManager<MdlSessionCustomerInfo>.GetSessionObject(SessionKey.UserInfo);
if (userInfo != null)
ret = true;
return ret;
}
}
}
復(fù)制代碼代碼如下:
public class BasePage : System.Web.UI.Page