牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2008-08-24 投稿人: 浏览次数:4650

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

    }

}

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。