2014-06-09 218 views
2

單擊按鈕時,我創建了一個Session["smn"]如何在JavaScript中清除會話?

我希望Session在彈出控件關閉時清除。

<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> 

我怎樣才能清除Session["smn"]ClearSession()功能?

回答

3

如果你需要清除您的CloseButtonClick事件上彈出關閉會話,那麼你可以使用一個隱藏按鈕,您的網頁上,並使用javascript觸發該事件以清除像下面您的會話:

1在你的頁面

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

2.添加這個腳本。新增CSS

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

3.添加一個隱藏的控制這樣一個

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

4.增加這一個在後面的代碼

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

現在在你的控制器上L您只需要調用以上的JavaScript,幾乎和你的一樣:

<ClientSideEvents CloseButtonClick="ClearSession()" /> 
+0

謝謝。這是好建議 – user3107343

+0

是的,這是一個很好的解決方案,但 - user3107343 - 您是否瞭解會話狀態不存在*到您網頁中的JavaScript? – sh1rts

+1

-1 to @ sh1rts你有什麼要說的? OP沒有要求在JavaScript中使用會話。他只是想用JavaScript清除會話 –

1

你需要告訴服務器殺死一個會話變量。

你不能用javascript單獨終止會話。對服務器進行AJAX調用並將該變量設置爲null

3

會話變量存儲在服務器端。有很多方法可以清除服務器端會話端。

以下是創建ClearSession.ashx(通用處理程序)的一種方法。要清除服務器端會話狀態,請從javascript中調用ClearSession.ashx

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

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