如何清除 JavaScript 中的会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24129136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear Session in JavaScript?
提问by user3107343
I create a Session["smn"]
when click a button.
我Session["smn"]
在单击按钮时创建一个。
I want Session will clear when popup control closed.
我希望 Session 在弹出控件关闭时清除。
<script type="text/javascript">
function ClearSession()
{
//clear Session["smn"];
}
</script>
ASPxPopupControl
ASPx弹出控件
<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 ?
如何清除Session["smn"]
的ClearSession()
功能?
回答by Win
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.
这是创建 ClearSession.ashx(通用处理程序)的一种方式。要清除服务器端会话状态,请ClearSession.ashx
从 javascript调用。
public class ClearSession : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Session["Date"] = null;
}
public bool IsReusable
{
get { return false; }
}
}
回答by Ali
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:
如果您需要在CloseButtonClick
事件中的弹出窗口关闭时清除会话,那么您可以使用页面上的隐藏按钮并使用 javascript 触发该事件以清除您的会话,如下所示:
1.Add CSS in your page
1.在页面中添加CSS
<style>.hidden { display: none; }</style>
<style>.hidden { display: none; }</style>
2.Add this script
2.添加这个脚本
function ClearSession() { document.getElementById("btnHidden").click(); }
function ClearSession() { document.getElementById("btnHidden").click(); }
3.Add a hidden control like this one
3.添加一个像这样的隐藏控件
<asp:Button ID="btnHidden" runat="server" CssClass="hidden" OnClick="Button1_Click" ClientIDMode="Static" />
<asp:Button ID="btnHidden" runat="server" CssClass="hidden" OnClick="Button1_Click" ClientIDMode="Static" />
4.Add this one in your code behind
4.在你的代码后面添加这个
protected void Button1_Click(object sender, EventArgs e) { Session["smn"] = null; }
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:
现在在您的控件上,您只需要调用上面的 javascript,与您的几乎相同:
<ClientSideEvents CloseButtonClick="ClearSession()" />
<ClientSideEvents CloseButtonClick="ClearSession()" />
回答by Josef E.
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
.
您不能单独使用 javascript 终止会话。对服务器进行 AJAX 调用并将变量设置为null
.