JavaScript - 检索会话变量值 [英] JavaScript - Retrieve session variable value

查看:70
本文介绍了JavaScript - 检索会话变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在aspx.cs中,我创建了一个会话变量:

In am aspx.cs, I created a session variable:

Session["test"] = "sss";



在另一个打开的页面中,我想通过


In another open page, I want to retrieve the value of the session variables in JS through

var s = '@Session["test"]';
var s2 = '<%= Session["test"] %>';  // got s2 value: <%=@Session["test"]%>
var s3 = '<%= session.getAttribute["test"] %>';



但什么也没得到。我该怎么做才能检索会话变量的值?谢谢。


But got nothing. What should I do to retrieve the session variable's value? Thanks.

推荐答案

您需要了解asp.net页面生命周期。您的代码正在做的是在首次从服务器请求并将该静态html发送到客户端时生成html。因此,如果您在页面被请求时在Session [test]中有Hello,那么它将生成以下js;



You need to understand the asp.net page lifecycle. What your code is doing is generating html when it is first requested from the server and sending that static html to the client. So if you had "Hello" in Session["test"] when the page was requested then it would generate the following js;

var s = 'Hello';





这是在浏览器中执行的内容,因此如果在其他地方修改了Session变量,则更改不会反映在原始页面中直到该页面被刷新,因为你的.net代码没有在浏览器中运行,它只用于生成发送到浏览器的初始html。



That is what is executed in the browser so if the Session variable is amended elsewhere that change is not going to reflect in your original page until that page is refreshed, as your .net code isn't "running inside" the browser, it is only used to generate the initial html to send to the browser.


你的第二个解决方案会做。请参阅更新解决方案的链接:



可以在javascript中获取会话值 [ ^ ]



asp.net - 在javascript中获取会话值 - Stack Overflow [ ^ ]
Your second solution will do. please see links to update your solution:

Possible to get session value in javascript[^]

asp.net - Getting session value in javascript - Stack Overflow[^]


这篇关于JavaScript - 检索会话变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆