To clear the Session, you must return to the server. The content of the server cannot be changed on the client. ASP. NET can be used to close the dialog layer of the Page Server. First, we need to determine when the page is closed to perform the next action. However, html dom does not need to close pages. Only onunload and onbeforeunload are related to ASP. NET, ASP. NET to close the page or refresh the event, onbeforeunload is ASP. NET to close the page or refresh the previous event, so we need to use onbeforeunload. Determine whether the user is closing the page or refreshing the page. The Code is as follows:
- Window. onbeforeunload = function ()
- {
- // This is found online. It has not been verified
- Var n = window.Event. ScreenX-window. screenLeft;
- Var B = n> document.doc umentElement. scrollWidth-20;
- If(B & window.Event. ClientY <0 | window.Event. AltKey)
- {
- ClearSession ();
- }
- }
ClearSession () is an ajax call request to the server. After receiving the request, the server clears the Session. The following is the code.
- ======================================= Default. aspx start ================================================= =====
-
- <% @ 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> No title page </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 ="Button#click"Text ="Write Session"/> </Div>
- </Form>
- </Body>
- </Html>
- ======================================= Default. aspx end ================================================= =====
-
-
- ======================================= Default. aspx. cs start =================================================== =====
-
- UsingSystem;
- UsingSystem. Data;
- UsingSystem. Configuration;
- UsingSystem. Web;
- UsingSystem. Web. Security;
- UsingSystem. Web. UI;
- UsingSystem. Web. UI. WebControls;
- UsingSystem. Web. UI. WebControls. WebParts;
- UsingSystem. Web. UI. HtmlControls;
- PublicPartialClass_ Default: System. Web. UI. Page
- {
- Protected VoidPage_Load (ObjectSender, EventArgs e)
- {
- If(!String. IsNullOrEmpty (Request. QueryString ["___ Command"])
- {
- StringCmd = Request. QueryString ["___ Command"];
- If(Cmd ="ClearSession")
- Session. Remove ("Name");// Clear the Session
- }
- If(Session ["Name"]! =Null)
- This. Label1.Text = Session ["Name"]. ToString ();
- }
- Protected VoidButton#click (ObjectSender, EventArgs e)
- {
- Session ["Name"] ="Vvvvvvvvvvvvv";
- If(Session ["Name"]! =Null)
- This. Label1.Text = Session ["Name"]. ToString ();
- }
- }
- ======================================= Default. aspx. cs end ===================================================== =====
-
-
-
-
- ===================================== Script. js start = =====
- Function GetXmlHttpObject ()
- {
- // Create an XMLHttpRequest object to send and receive HTTP requests and responses
- XmlHttpObj =Null;
- Try
- {
- // FireFox Opera 8.0 + Safari
- XmlHttpObj =NewXMLHttpRequest ();
- If(XmlHttpObj. overrideMimeType)
- {
- XmlHttpObj. overrideMimeType ('Text/xml');
- }
- }
- Catch(E)
- {
- // IE
- Try
- {
- XmlHttpObj =NewActiveXObject ("Msxml2.XMLHTTP");
- }
- Catch(E)
- {
- XmlHttpObj =NewActiveXObject ("Microsoft. XMLHTTP");
- }
- }
- ReturnXmlHttpObj;
- }
- 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.doc umentElement. scrollWidth-20;
- If(B & window.Event. ClientY <0 | window.Event. AltKey)
- {
- ClearSession ();
- }
- }
This article is from: blog Author: Chen yuexiong
- Disable ASP. NET midway through to check whether it affects server execution
- Disable the ASP. net bug in the browser sessionfor 1 minute
- Overview ASP. NET page framework
- Describe ASP. NET page forms
- ASP. NET page Object Model