vb.net 如何在 javascript 中获取 ASP.NET 会话变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14580311/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:05:21  来源:igfitidea点击:

how can i get a ASP.NET session variable in javascript?

asp.netvb.netsession

提问by Eray Geveci

i have a website a with some textfields.

我有一个带有一些文本字段的网站。

website b is in the iframe of a.

网站 b 在 a 的 iframe 中。

I saved in website b some values in different sessions

我在网站 b 中保存了不同会话中的一些值

  Session("anzahlInterneTeilnehmer") = anzahlInterneTeilnehmer
  Session("anzahlExterneTeilnehmer") = anzahlExterneTeilnehmer

after i saved the sessions i call the parent function from website a

保存会话后,我从网站 a 调用父函数

parent.parentTeilnehmer()

i want to display the session values in the textfields of website a

我想在网站 a 的文本字段中显示会话值

            function parentTeilnehmer() {
            var intern = ????
            var extern = ????
            var InterneTextFeld = ISGetObject("WebInput1");
            var ExterneTextFeld = ISGetObject("WebInput2");
            InterneTextFeld.SetValueData(intern);
            ExterneTextFeld.SetValueData(extern);
        }

how can i do that without refreshing the whole page?

我怎么能在不刷新整个页面的情况下做到这一点?

回答by Riain McAtamney

Session is stored server side so you cannot access these variables clientside via javascript. I suggest saving these variables to a hidden field

会话存储在服务器端,因此您无法通过 javascript 访问这些变量客户端。我建议将这些变量保存到隐藏字段

<input type="hidden" id="anzahlInterneTeilnehmer" runat="server" />
<input type="hidden" id="anzahlExterneTeilnehmer" runat="server" />

Attach an event handler to the Website A's page PreRender method and in there set the anzahlInterneTeilnehmer's value to the session value (excuse my VB)

将事件处理程序附加到网站 A 的页面 PreRender 方法,并在其中将 anzahlInterneTeilnehmer 的值设置为会话值(请原谅我的 VB)

Private Sub Page_Render(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
anzahlInterneTeilnehmer.Value = Session("anzahlInterneTeilnehmer")
End Sub

Then in your javascript

然后在你的javascript中

var internHolder = document.getElementById("anzahlInterneTeilnehmer");
var intern = internHolder.value;

回答by Nag

You can do like this in javascript

你可以在javascript中这样做

var val = <%= Session["MyVariable"]%>; 

回答by Coder

You can access Session variables in javascript only if your js function is present on .aspxpage which is not a good approach as it is better to keep our script files in separate Scripts folder so you can store session values in hidden fields:

只有当您的 js 函数存在于.aspx页面上时,您才能在 javascript 中访问会话变量,这不是一个好方法,因为最好将我们的脚本文件保存在单独的 Scripts 文件夹中,以便您可以将会话值存储在隐藏字段中:

<script type="text/javascript">
    function GetSessionValues() {
        var intern = '<%=Session("InternalValue") %>';
        var extern = '<%=Session("ExternalValue") %>';
    }
</script>