how to clear session variable after a user control unload

reza akhlaghi

i am using a session variable in a usercontrol (ascx), how can i remove that from session when user close webpage or redirect to other pages?

user2771704

For redirect you can use this code when redirected page/view loaded:

if (!IsPostBack)
{
   Session.Clear(); //if you want clear session
   Session.Remove("myVar");//if you want clear just 1 session variable
}

With closing the page situation is harder, because HTTP is a stateless protocol, so you server does not know if user closed their browser or they just simply left an opened browser window for long time.

Уou can use Ajax to handle clear session on tab close like below.

 <body onunload="unlodFunc()">

    <script>
    function unlodFunc()
    {
      $.ajax({
        type: "POST",
        url: "MyPage.aspx/ClearSession",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

        }
    });
    }
    </script>

C# code:

[WebMethod]
public static void ClearSession()
{
    if (Session["myVar"] != null)
    {
        Session.Remove("myVar");
    }
}

Also you can check this link.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to clear a session variable once the user logout in laravel 5.2 auth system

How to unload multiple user forms

exitBlockOnFail or clear session to control a flow

How to clear a variable after one run in a for loop

How to load/unload a graph from a session in tensorflow

Laravel How to clear session after display in specific page

How to clear the session and only visit an about page after login?

how to maintain user session after app is updated

How to clear a session cookie

How to fix session not starting after unsetting a variable

How to unload jQuery plugins after window resize?

How to mantain user session after user close the app with framework 7?

How to clear a variable's memory, after the function is called the first time

how to clear all session on byobu

How to clear session storage in angularjs?

Clear session after checkout with custom payment method

How do I set a user environment variable? (permanently, not session)

How to store user session after signup in remix-auth?

AWS Amplify: How to expire user session after certain time of inactivity?

How to destroy a session an hour or day after a user exit site

How to get user data after login in php with session

how to update a session variable in after the reload of every page in laravel

How to call an AJAX function after setting a session variable?

How to delete session variable after use in Django templates

Populate user session after login

grid view user control uses session

Google Webfonts: how to unload fonts after loading them?

How do I unload a library after it is LD_PRELOAD'ed?

pass variable in as parameter to user control