退出页面时清空全部或指定Session变量

来源:互联网 发布:windows系统官网 编辑:程序博客网 时间:2024/05/03 08:17

1HTML代码

<%@ 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">   

  <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>

2C#代码

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>");       

    }

}

原创粉丝点击