ASP NET从JavaScript读会话 [英] ASP NET read session from javascript

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

问题描述

我在Session对象中至极我店字符串数组的WebForms的应用程序,我需要
得到这个数组中的JavaScript code。也许有人可以提供任何解决方案,我应该怎么做呢?

I have a WebForms application in wich I store array of strings in Session object and I need to get this array in javascript code. Maybe someone can provide any solution, how should i do this?

下面是我的code:

    function loadAnswers() {
        var answers = '<%=Session("answers")%>';
    }

但不工作,answerss变量包含转让之后简单的字符串。 ('System.String []')

but is doesn't work and answerss variable contain simple string after assignment. ('System.String[]')

推荐答案

(我假设u使用VB)您可以使用code是这样的:

(I assume u use VB) You can use a code like this:

function loadAnswers() {
   var answers = ['<%= String.Join("','", CType(Session("answers"), String()))%>'];
}

它实际上加入阵列成一个字符串,被格式化成JS数组重新presentation。

It actually joins your array into a string, that is formatted into JS array representation.

因此​​,举例来说,如果你有一个这样的数组

So for example if you have an array like this

Session("answers") = New String() {"aaa", "bbb", "ccc"}

这将prodice这样的行

It will prodice a line like this

var answers = ['aaa','bbb','ccc'];

这将是一个真正的JS数组。如果你需要一个简单的字符串,而不是,你可以用

Which will be a real JS array. If you need a simple string instead, you can use

var answers = '<%= String.Join(",", CType(Session("answers"), String()))%>';

这将产生

 var answers = 'aaa,bbb,ccc';

这篇关于ASP NET从JavaScript读会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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