How can I clear asp.net session value in OnUnload event in javascript


I'd like to clear ASP.NET session value when browser closed. I've onunload function defined in my html page, but I don't know how can I clear session value in this Javascript method ?

<body onunload="myUnloadFunction()"> <script> function myUnloadFunction() { // Needs asp.net session code here } </script>

Please let me know Is it possible else any other preferable way of doing.


Try this:


[WebMethod] public static void ClearYourSessionValue() { // This will leave the key in the Session cache, but clear the value Session["YourKey"] = null; // This will remove both the key and value from the Session cache Session.Remove("YourKey"); }


$.ajax({ type: "POST", url: "PageName.aspx/ClearYourSessionValue", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { // Do something interesting here. } });

If you want more information about ASP.NET AJAX Page Methods and how to invoke them via AJAX then read <a href="http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/" rel="nofollow">Using jQuery to directly call ASP.NET AJAX page methods</a>


  • Get Proper Progress Updates on Two Long Waited Concurrent Processes in ASP.NET
  • jQuery Ajax setTimeout JSON
  • jax-ws Undefined port type with client and server in separate projects
  • WebMethod returning generic list
  • How do I wrap a span around a section of text in javascript?
  • Best modern way of creating indentation in a ?
  • Grails eager fetch doesn't retrieve all data
  • C# Remove URL from String
  • Catching exception and throwing the same?
  • Entity Framework ObjectContext: Concurrency
  • Want to understand iframe breakout code
  • C# List of Panels
  • How to return DataSet (xsd) in WCF
  • Custom validator control occupying space even though display set to dynamic
  • Moving mysql files across servers
  • DomPDF {PAGE_NUM} not on first page
  • ilmerge with a PFX file
  • Why value captured by reference in lambda is broken? [duplicate]
  • Javascript simulate pressing enter in input box
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • Importing jscolor library in angular 2
  • Do I've to free mysql result after storing it?
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • A cron job substitute?
  • json Serialization in asp
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • Why joiner is not used after Sequence generator or Update statergy
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Recursive/Hierarchical Query Using Postgres
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal