ASP. NET close Page Server dialog layer clear

Source: Internet
Author: User
Tags close page net bug

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:

   
 
  1. Window. onbeforeunload = function ()
  2. {
  3.  // This is found online. It has not been verified 
  4. Var n = window.Event. ScreenX-window. screenLeft;
  5. Var B = n> document.doc umentElement. scrollWidth-20;
  6. If(B & window.Event. ClientY <0 | window.Event. AltKey)
  7. {
  8. ClearSession ();
  9. }
  10. }

ClearSession () is an ajax call request to the server. After receiving the request, the server clears the Session. The following is the code.

 
 
  1. ======================================= Default. aspx start ================================================= =====
  2.  
  3. <% @ Page Language ="C #"AutoEventWireup ="True"CodeFile ="Default. aspx. cs"Inherits ="_ Default"%>
  4. <! DOCTYPE html PUBLIC"-// W3C // dtd xhtml 1.0 Transitional // EN" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <Html xmlns =Http://www.w3.org/1999/xhtml">
  6. <Head runat ="Server">
  7. <Title> No title page </title>
  8. <Script type ="Text/javascript"Src ="Script. js"> </Script>
  9. </Head>
  10. <Body>
  11. <Form id ="Form1"Runat ="Server">
  12. <Div>
  13. <Asp: Label ID ="Label1"Runat ="Server"> </Asp: Label>
  14. <Asp: Button ID ="Button1"Runat ="Server"OnClick ="Button#click"Text ="Write Session"/> </Div>
  15. </Form>
  16. </Body>
  17. </Html>
  18. ======================================= Default. aspx end ================================================= =====
  19.  
  20.  
  21. ======================================= Default. aspx. cs start =================================================== =====
  22.  
  23. UsingSystem;
  24. UsingSystem. Data;
  25. UsingSystem. Configuration;
  26. UsingSystem. Web;
  27. UsingSystem. Web. Security;
  28. UsingSystem. Web. UI;
  29. UsingSystem. Web. UI. WebControls;
  30. UsingSystem. Web. UI. WebControls. WebParts;
  31. UsingSystem. Web. UI. HtmlControls;
  32. PublicPartialClass_ Default: System. Web. UI. Page
  33. {
  34. Protected VoidPage_Load (ObjectSender, EventArgs e)
  35. {
  36. If(!String. IsNullOrEmpty (Request. QueryString ["___ Command"])
  37. {
  38. StringCmd = Request. QueryString ["___ Command"];
  39. If(Cmd ="ClearSession")
  40. Session. Remove ("Name");// Clear the Session 
  41. }
  42. If(Session ["Name"]! =Null)
  43. This. Label1.Text = Session ["Name"]. ToString ();
  44. }
  45. Protected VoidButton#click (ObjectSender, EventArgs e)
  46. {
  47. Session ["Name"] ="Vvvvvvvvvvvvv";
  48. If(Session ["Name"]! =Null)
  49. This. Label1.Text = Session ["Name"]. ToString ();
  50. }
  51. }
  52. ======================================= Default. aspx. cs end ===================================================== =====
  53.  
  54.  
  55.  
  56.  
  57. ===================================== Script. js start = =====
  58. Function GetXmlHttpObject ()
  59. {
  60. // Create an XMLHttpRequest object to send and receive HTTP requests and responses 
  61. XmlHttpObj =Null;
  62. Try
  63. {
  64. // FireFox Opera 8.0 + Safari 
  65. XmlHttpObj =NewXMLHttpRequest ();
  66. If(XmlHttpObj. overrideMimeType)
  67. {
  68. XmlHttpObj. overrideMimeType ('Text/xml');
  69. }
  70. }
  71. Catch(E)
  72. {
  73. // IE 
  74. Try
  75. {
  76. XmlHttpObj =NewActiveXObject ("Msxml2.XMLHTTP");
  77. }
  78. Catch(E)
  79. {
  80. XmlHttpObj =NewActiveXObject ("Microsoft. XMLHTTP");
  81. }
  82. }
  83. ReturnXmlHttpObj;
  84. }
  85. Function StateChanged ()
  86. {
  87. If(___ XmlHttp. readyState = 4)
  88. {
  89. If(___ XmlHttp. status = 200)
  90. {
  91. }
  92. Else
  93. {
  94. }
  95. }
  96. }
  97. Var ___ xmlHttp =Null;
  98. Function ClearSession ()
  99. {
  100. If(___ XmlHttp =Null)
  101. ___ XmlHttp = GetXmlHttpObject ();
  102. If(___ XmlHttp =Null)
  103. Return False;
  104. Var url ="? ___ Command = ClearSession & ___ clientRandom ="+ Math. random ();
  105. ___ XmlHttp. open ("GET", Url,True);
  106. ___ XmlHttp. onreadystatechange = StateChanged;
  107. ___ XmlHttp. send (Null);
  108. }
  109. Window. onbeforeunload = function ()
  110. {
  111. Var n = window.Event. ScreenX-window. screenLeft;
  112. Var B = n> document.doc umentElement. scrollWidth-20;
  113. If(B & window.Event. ClientY <0 | window.Event. AltKey)
  114. {
  115. ClearSession ();
  116. }
  117. }

This article is from: blog Author: Chen yuexiong

  1. Disable ASP. NET midway through to check whether it affects server execution
  2. Disable the ASP. net bug in the browser sessionfor 1 minute
  3. Overview ASP. NET page framework
  4. Describe ASP. NET page forms
  5. ASP. NET page Object Model

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.