退出页面时清空全部或指定Session变量
1、HTML代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
<!DOCTYPE html PUBLIC "-//W<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />3C//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">
<script language="javascript">
g_blnCheckUnload = true;
function RunOnBeforeUnload()
{
if(g_blnCheckUnload)//或者if(g_blnCheckUnload==true)
{
window.event.returnValue ="You will lose any unsaved content";
//"You will lose any unsaved content"为该事件的返回值
<%Session.Remove("f");%>
//<%Session.RemoveAll();%>
}
//session.removeattribute("f");
//session.removevalue("f");
}
function bypassCheck()
{
g_blnCheckUnload = false;
}
</script>
<title>无标题页</title>
</head>
<body onbeforeunload="RunOnBeforeUnload()">
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" OnClientClick="bypassCheck()" Text="Button" /></div>
</form>
</body>
</html>
2、C#代码
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)
{
Session["f"] ="f";
}
protected void Button1_Click(object sender, EventArgs e)
{
//Response.Write("<script language=javascript>window.open("Default2.aspx");</scirpt>");
Response.Write("<script language=javascript>window.open("Default2.aspx","_blank");</script>");
}
}
- 上一篇: java数组和字符串相互转换
- 下一篇: 使用纯CSS实现文字超过一定的长度后自动显示省略号