Removing a .NET Session variable with JavaScript

Want to clear an ASP.NET session variable from JavaScript? Of course you do – all the cool developers are doing it. First, create a JavaScript function that uses an XMLHttpRequest class to make a web service request:

function clearSession() 
{ 

  var xmlVar = new XMLHttpRequest();  

  xmlVar .open("GET", "http://www.ScottsCoolSite.com/Service.asmx?Woohoo", false); 

  xmlVar .send(null);  

}

Yes, I named the service function name “Woohoo”.

And you’ll need a way to call your new function, so a button would be nice:

  <input type="button" id="btnClear" value="Clear Session" onclick="clearSession()" />

Finally, you’ll need a new Web Service file to the project called Service.asmx.  Inside the service file, add the best web service name ever:

 [WebMethod]
 public void Woohoo()
 {
    Session.Remove("MyVar");
 }

So there you go.

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: