user3107343
user3107343

Reputation: 2299

How to clear Session in JavaScript?

I create a Session["smn"] when click a button.

I want Session will clear when popup control closed.

   <script type="text/javascript">

    function ClearSession()
    {
        //clear Session["smn"];
    }

  </script>

ASPxPopupControl

 <dx:ASPxPopupControl ID="popup1" PopupHorizontalAlign="WindowCenter"  runat="server" 
 ClientInstanceName="popup1"  PopupVerticalAlign="WindowCenter" ShowPinButton="true"    
 ShowCollapseButton="true" CloseAction="CloseButton" AllowDragging="True" Width="800px" Height="600px">
        <ContentCollection>
            <dx:PopupControlContentControl ID="PopupControlContentControl1" runat="server">



            </dx:PopupControlContentControl>
        </ContentCollection>

    <ClientSideEvents CloseButtonClick="ClearSession" />
    </dx:ASPxPopupControl>

How can I clear Session["smn"] in ClearSession() function ?

Upvotes: 2

Views: 27473

Answers (3)

Ali
Ali

Reputation: 2592

if you need to clear the session on popup close in your CloseButtonClick event, then you can use a hidden button on your page and fire that event using javascript to clear your session like below:

1.Add CSS in your page

   <style>.hidden { display: none; }</style> 

2.Add this script

   function ClearSession() { document.getElementById("btnHidden").click(); }

3.Add a hidden control like this one

   <asp:Button ID="btnHidden" runat="server" CssClass="hidden" 
                 OnClick="Button1_Click" ClientIDMode="Static" />

4.Add this one in your code behind

   protected void Button1_Click(object sender, EventArgs e) { Session["smn"] = null; }

Now on your control you just need to call the javascript above, pretty much the same as yours:

   <ClientSideEvents CloseButtonClick="ClearSession()" />

Upvotes: 3

Win
Win

Reputation: 62260

Session variables are stored at server side. There are lot of way to clear Server Side session side.

Here is one way in which you create ClearSession.ashx (generic handler). To clear server side session state, you call ClearSession.ashx from javascript.

public class ClearSession : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Session["Date"] = null;
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

Upvotes: 3

Josef E.
Josef E.

Reputation: 2219

You need to tell the server to kill a session variable.

You cannot kill the session with javascript alone. Make an AJAX call to the server and set the variable to null.

Upvotes: 2

Related Questions