Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP NET从javascript读取会话_Javascript_C#_Asp.net_Vb.net_Webforms - Fatal编程技术网

ASP NET从javascript读取会话

ASP NET从javascript读取会话,javascript,c#,asp.net,vb.net,webforms,Javascript,C#,Asp.net,Vb.net,Webforms,我有一个WebForms应用程序,我在Session对象中存储字符串数组,我需要 在javascript代码中获取此数组。也许有人能提供任何解决方案,我该怎么做 这是我的密码: function loadAnswers() { var answers = '<%=Session("answers")%>'; } 函数loadAnswers(){ var回答=“”; } 但is不起作用,且answerss变量在赋值后包含简单字符串。('System.

我有一个WebForms应用程序,我在Session对象中存储字符串数组,我需要 在javascript代码中获取此数组。也许有人能提供任何解决方案,我该怎么做

这是我的密码:

    function loadAnswers() {
        var answers = '<%=Session("answers")%>';
    }
函数loadAnswers(){
var回答=“”;
}

但is不起作用,且answerss变量在赋值后包含简单字符串。('System.String[])

如果您将
answers
对象序列化为JSON,那么您应该能够通过JavaScript以编程方式访问它

var answers = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Session("answers")) %>;
var-answers=;

更新:下面是一个使用四种不同类型数据(字符串、数字、集合、对象)的工作示例。这说明了如何将从服务器序列化的JSON用作JavaScript对象文本客户端

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFormsTestApp
{
    public partial class _Default : Page
    {
        protected string Name = "Alice Student";
        protected decimal GPA = 3.84M;
        protected List<string> Classes = new List<string>() { "World History", "Algebra II", "English", "Phys Ed", "Latin I", "Home Economics" };
        protected School School = new School() { Name = "Jefferson High School", County = "Hamilton County", Ranking = 5 };
    }

    public class School
    {
        public string Name { get; set; }
        public string County { get; set; }
        public int Ranking { get; set; }
    }
}
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebFormsTestApp._Default" %>

<script type="text/javascript">
    var name = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Name) %>;
    var gpa = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(GPA) %>;
    var classes = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Classes) %>;
    var school = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(School) %>;
    alert(name + ' goes to ' + school.Name + ', has a ' + gpa + ' GPA, and takes ' + classes.length + ' classes.');
</script>
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间WebFormsTestApp
{
公共部分类\u默认值:第页
{
受保护的字符串名称=“Alice Student”;
保护十进制GPA=3.84M;
受保护列表类=新列表({“世界历史”、“代数II”、“英语”、“物理教育”、“拉丁语I”、“家政学”});
受保护学校=新学校(){Name=“杰斐逊高中”,County=“汉密尔顿县”,排名=5};
}
公立学校
{
公共字符串名称{get;set;}
公共字符串country{get;set;}
公共整数排序{get;set;}
}
}
ASPX:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFormsTestApp
{
    public partial class _Default : Page
    {
        protected string Name = "Alice Student";
        protected decimal GPA = 3.84M;
        protected List<string> Classes = new List<string>() { "World History", "Algebra II", "English", "Phys Ed", "Latin I", "Home Economics" };
        protected School School = new School() { Name = "Jefferson High School", County = "Hamilton County", Ranking = 5 };
    }

    public class School
    {
        public string Name { get; set; }
        public string County { get; set; }
        public int Ranking { get; set; }
    }
}
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebFormsTestApp._Default" %>

<script type="text/javascript">
    var name = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Name) %>;
    var gpa = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(GPA) %>;
    var classes = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Classes) %>;
    var school = <%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(School) %>;
    alert(name + ' goes to ' + school.Name + ', has a ' + gpa + ' GPA, and takes ' + classes.length + ' classes.');
</script>

变量名=;
var-gpa=;
var类=;
var学校=;
警报(name+'转到“+school.name+”,具有“+gpa+”gpa,并接受“+classes.length+”classes.”);
(我假设您使用VB)您可以使用如下代码:

function loadAnswers() {
   var answers = ['<%= String.Join("','", CType(Session("answers"), String()))%>'];
}
它将产生一条像这样的线

Session("answers") = New String() {"aaa", "bbb", "ccc"}
var answers = ['aaa','bbb','ccc'];
这将是一个真正的JS数组。如果您需要一个简单的字符串,可以使用

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

您正在将答案初始化为字符串。有点像执行“var answers='some value';”,您必须像javascript中的任何其他数组一样初始化和填充答案。“var answers=['a1','a2',…];”必须确保答案不包含任何“@the_lotus yup,额外的检查/转换可能是有序的,至少在结果stringthx中省略撇号\!”!它可以工作。-1:
newstring(){“混合O'quotes”“successed”“”}
。使用字符串连接生成正确的JS/HTML/XML是一件非常痛苦的事情——尽量不要这样做。@YuriyGalanter:)你对如此多的访问者期望太高了……谢谢!这很有帮助!