要清掉Session必须回到服务端,在客户端是不能改变服务端内容的。ASP.NET 关闭页面服务端对话层的清空我们可以变通下——使用ajax。首先我们要判断用户什么时候关闭了页面,这样才能执行下一步动作。不过HTML DOM没要页面关闭的事件,只有onunload和onbeforeunload是与ASP.NET 关闭页面有关的,ASP.NET 关闭页面或刷新后的事件,onbeforeunload是ASP.NET 关闭页面或刷新前的事件,所以我们要用的是onbeforeunload。要判断下用户是关闭页面还是在刷新页面。代码如下:
- window.onbeforeunload = function()
- {
- //这是网上找的,具体没验证过
ClearSession()为ajax调用请求服务端,服务端接收到请求后执行清空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>无标题页</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 结束===========================================
- ========================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 结束===========================================
- ========================script.js 开始===========================================
- function GetXmlHttpObject()
- {
- //创建XMLHttpRequest对象来发送和接收HTTP请求与响应
- 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();
- }
- }
本文来自:博客 作者:陈粤雄
【编辑推荐】