ASP.NET 關(guān)閉頁面服務(wù)端對(duì)話層清空
要清掉Session必須回到服務(wù)端,在客戶端是不能改變服務(wù)端內(nèi)容的。ASP.NET 關(guān)閉頁面服務(wù)端對(duì)話層的清空我們可以變通下——使用ajax。首先我們要判斷用戶什么時(shí)候關(guān)閉了頁面,這樣才能執(zhí)行下一步動(dòng)作。不過HTML DOM沒要頁面關(guān)閉的事件,只有onunload和onbeforeunload是與ASP.NET 關(guān)閉頁面有關(guān)的,ASP.NET 關(guān)閉頁面或刷新后的事件,onbeforeunload是ASP.NET 關(guān)閉頁面或刷新前的事件,所以我們要用的是onbeforeunload。要判斷下用戶是關(guān)閉頁面還是在刷新頁面。代碼如下:
- window.onbeforeunload = function()
- {
- //這是網(wǎng)上找的,具體沒驗(yàn)證過
ClearSession()為ajax調(diào)用請求服務(wù)端,服務(wù)端接收到請求后執(zhí)行清空Session的操作。Ajax的東西不多說了,下面為代碼。
- ========================Default.aspx 開始===========================================
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>無標(biāo)題頁</title>
- <script type="text/javascript" src="script.js"></script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Label ID="Label1" runat="server"></asp:Label>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="寫入Session" /></div>
- </form>
- </body>
- </html>
- ========================Default.aspx 結(jié)束===========================================
- ========================Default.aspx.cs 開始===========================================
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!string.IsNullOrEmpty(Request.QueryString["___command"]))
- {
- string cmd = Request.QueryString["___command"];
- if (cmd == "ClearSession")
- Session.Remove("name");//清空Session
- }
- if (Session["name"] != null)
- this.Label1.Text = Session["name"].ToString();
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Session["name"] = "vvvvvvvvvvvvv";
- if (Session["name"] != null)
- this.Label1.Text = Session["name"].ToString();
- }
- }
- ========================Default.aspx.cs 結(jié)束===========================================
- ========================script.js 開始===========================================
- function GetXmlHttpObject()
- {
- //創(chuàng)建XMLHttpRequest對(duì)象來發(fā)送和接收HTTP請求與響應(yīng)
- xmlHttpObj = null;
- try
- {
- // FireFox Opera 8.0+ Safari
- xmlHttpObj = new XMLHttpRequest();
- if(xmlHttpObj.overrideMimeType)
- {
- xmlHttpObj.overrideMimeType('text/xml');
- }
- }
- catch(e)
- {
- // IE
- try
- {
- xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
- }
- catch(e)
- {
- xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
- }
- }
- return xmlHttpObj;
- }
- function StateChanged()
- {
- if(___xmlHttp.readyState == 4)
- {
- if(___xmlHttp.status == 200)
- {
- }
- else
- {
- }
- }
- }
- var ___xmlHttp=null;
- function ClearSession()
- {
- if(___xmlHttp==null)
- ___xmlHttp = GetXmlHttpObject();
- if(___xmlHttp == null)
- return false;
- var url = "?___command=ClearSession&___clientRandom=" + Math.random();
- ___xmlHttp.open("GET", url, true);
- ___xmlHttp.onreadystatechange = StateChanged;
- ___xmlHttp.send(null);
- }
- window.onbeforeunload = function()
- {
- var n = window.event.screenX - window.screenLeft;
- var b = n > document.documentElement.scrollWidth-20;
- if(b && window.event.clientY < 0 || window.event.altKey)
- {
- ClearSession();
- }
- }
本文來自:博客 作者:陳粵雄
【編輯推薦】