从JavaScript访问ASP.NET会话变量 [英] Access ASP.NET session variable from JavaScript
问题描述
当code VAR ACCNO ='<%=会话[hdnAccession]%GT;';
将被执行?我更改 Page_LoadComplete
事件的会话变量,但是当我访问它使用 VAR ACCNO ='<%=会话[hdnAccession]% >';
,它总是返回我首先设置的值。在 Page_LoadComplete
,我喜欢下面... 会话[hdnAccession] = GetNewAccession()
,当我调试时,我看到会话[hdnAccession]
每次更新。但它为什么没有得到在JavaScript中更新?我是在一个情况下,我不能使用 HiddenField
而不是 Seession
。
When the code var accNo = '<%=Session["hdnAccession"]%>';
will be executed ?. I change the session variable in the Page_LoadComplete
event, but when I access it using var accNo = '<%=Session["hdnAccession"]%>';
, it always return the value which I set FIRST. In Page_LoadComplete
, I do like the following... Session["hdnAccession"] = GetNewAccession()
, When I debugged, I saw that the Session["hdnAccession"]
updated each time. But why it do not get updated in JavaScript ?. I am in a situation where I can not use HiddenField
instead of Seession
.
推荐答案
您需要创建一个回传,从JS访问会话变量。像这样:
You need to create a PostBack to access session variables from JS. Like so:
<script type="text/javascript">
<!--
function setSessionVariable(valueToSetTo)
{
__doPostBack('SetSessionVariable', valueToSetTo);
}
// -->
</script>
private void Page_Load(object sender, System.EventArgs e)
{
// Insure that the __doPostBack() JavaScript method is created...
this.GetPostBackEventReference(this, string.Empty);
if ( this.IsPostBack )
{
string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
if ( eventTarget == "SetSessionVariable" )
{
Session["someSessionKey"] = eventArgument;
}
}
}
在这里看到: http://forums.asp.net/post/2230824.aspx一>
这篇关于从JavaScript访问ASP.NET会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!