Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.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
在ASPX的代码隐藏中调用列表,在foreach循环的javascript代码中调用列表?_Javascript_C#_Asp.net_.net_Visual Studio 2012 - Fatal编程技术网

在ASPX的代码隐藏中调用列表,在foreach循环的javascript代码中调用列表?

在ASPX的代码隐藏中调用列表,在foreach循环的javascript代码中调用列表?,javascript,c#,asp.net,.net,visual-studio-2012,Javascript,C#,Asp.net,.net,Visual Studio 2012,我有以下aspx.cs: public partial class BarChart { public class LabelsDetail { public string LabelId { get;set; } public string LabelDesc { get; set; } } public List<LabelsDetail> LabelsDetails { get; set; } publ

我有以下aspx.cs:

public partial class BarChart 
{
    public class LabelsDetail
    {
        public string LabelId { get;set; }
        public string LabelDesc { get; set; }
    }
    public List<LabelsDetail> LabelsDetails { get; set; }

    public void InsertDataToLabelsDetails()
    {
        // Data comes from somewhere into "LabelsDetails"

    }

}
公共部分类条形图
{
公共类标签详细信息
{
公共字符串LabelId{get;set;}
公共字符串LabelDesc{get;set;}
}
公共列表标签详细信息{get;set;}
public void InsertDataToLabelsDetails()
{
//数据来源于“标签细节”
}
}
以及ASPX页面中的以下JS代码:

        function setupBarChart(JQObjectContainer, JsonData) {
            var hashTableSize = <%=this.LabelsDetails.Count%>;
            var hashtable = {};

            if (hashTableSize != 'undefined' && hashTableSize > 0)
            {
                for (var item in <%=this.LabelsDetails%>)
                { 
                    hashtable[item.LabelId] = item.LabelDesc;
                }
            }

}
函数设置条形图(JQObjectContainer,JsonData){
var hashTableSize=;
var hashtable={};
if(hashTableSize!=“未定义”&&hashTableSize>0)
{
用于(中的var项目)
{ 
hashtable[item.LabelId]=item.LabelDesc;
}
}
}
如何在客户端的服务器端列表上执行foreach

此时,我得到了
未捕获的语法错误:未终止的模板文本

当我尝试循环服务器端列表时(
this.LabelsDetails


谢谢

您必须将您的集合转换为JavaScript能够理解的符号,才能使用或任何其他JSON转换器:

var collection = new[]{
    new { Name = "a" },
    new { Name = "b" },
    new { Name = "c" },
    new { Name = "d" }
};

System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();

Console.WriteLine(s.Serialize(collection));
这将输出
[{“Name”:“a”},{“Name”:“b”},{“Name”:“c”},{“Name”:“d”}]
这是JavaScript的有效数组表示法。您还可以改进在JS中迭代的方式:

var array = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(this.LabelsDetails)%>;
for(var x=0;x<array.length;x++)
{ 
    hashtable[array[x].LabelId] = array[x].LabelDesc;
}
var数组=;
对于(变量x=0;x:

数组迭代和…中的

注意:for..in不应用于迭代 索引顺序很重要。数组索引只是可枚举的属性 具有整数名称,并且在其他方面与常规对象相同 不保证for…in会退还 索引的任何特定顺序,它将返回所有可枚举的 属性,包括具有非整数名称的属性和 继承的


但是,可能值得回顾一下您的方法,并使用其他技术来实现客户端和服务器端之间的连接。

您必须将集合转换为JavaScript能够理解的符号,才能使用或任何其他JSON转换器:

var collection = new[]{
    new { Name = "a" },
    new { Name = "b" },
    new { Name = "c" },
    new { Name = "d" }
};

System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();

Console.WriteLine(s.Serialize(collection));
这将输出
[{“Name”:“a”},{“Name”:“b”},{“Name”:“c”},{“Name”:“d”}]
这是JavaScript的有效数组符号。您还可以改进在JS中迭代的方式:

var array = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(this.LabelsDetails)%>;
for(var x=0;x<array.length;x++)
{ 
    hashtable[array[x].LabelId] = array[x].LabelDesc;
}
var数组=;
对于(变量x=0;x:

数组迭代和…中的

注意:for..in不应用于迭代 索引顺序很重要。数组索引只是可枚举的属性 具有整数名称,并且在其他方面与常规对象相同 不保证for…in会退还 索引的任何特定顺序,它将返回所有可枚举的 属性,包括具有非整数名称的属性和 继承的


但是,可能值得回顾一下您的方法,并使用其他技术来实现客户端和服务器端之间的连接。

您必须将集合转换为JavaScript能够理解的符号,才能使用或任何其他JSON转换器:

var collection = new[]{
    new { Name = "a" },
    new { Name = "b" },
    new { Name = "c" },
    new { Name = "d" }
};

System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();

Console.WriteLine(s.Serialize(collection));
这将输出
[{“Name”:“a”},{“Name”:“b”},{“Name”:“c”},{“Name”:“d”}]
这是JavaScript的有效数组符号。您还可以改进在JS中迭代的方式:

var array = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(this.LabelsDetails)%>;
for(var x=0;x<array.length;x++)
{ 
    hashtable[array[x].LabelId] = array[x].LabelDesc;
}
var数组=;
对于(变量x=0;x:

数组迭代和…中的

注意:for..in不应用于迭代 索引顺序很重要。数组索引只是可枚举的属性 具有整数名称,并且在其他方面与常规对象相同 不保证for…in会退还 索引的任何特定顺序,它将返回所有可枚举的 属性,包括具有非整数名称的属性和 继承的


但是,可能值得回顾一下您的方法,并使用其他技术来实现客户端和服务器端之间的连接。

您必须将集合转换为JavaScript能够理解的符号,才能使用或任何其他JSON转换器:

var collection = new[]{
    new { Name = "a" },
    new { Name = "b" },
    new { Name = "c" },
    new { Name = "d" }
};

System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();

Console.WriteLine(s.Serialize(collection));
这将输出
[{“Name”:“a”},{“Name”:“b”},{“Name”:“c”},{“Name”:“d”}]
这是JavaScript的有效数组符号。您还可以改进在JS中迭代的方式:

var array = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(this.LabelsDetails)%>;
for(var x=0;x<array.length;x++)
{ 
    hashtable[array[x].LabelId] = array[x].LabelDesc;
}
var数组=;
对于(变量x=0;x:

数组迭代和…中的

注意:for..in不应用于迭代 索引顺序很重要。数组索引只是可枚举的属性 具有整数名称,并且在其他方面与常规对象相同 不保证for…in会退还 索引的任何特定顺序,它将返回所有可枚举的 属性,包括具有非整数名称的属性和 继承的

但是,可能值得回顾一下您的方法,并使用其他技术来实现客户端和服务器端之间的连接。

试试这个

function setupBarChart(JQObjectContainer, JsonData) {
            var hashTableSize = <%=this.LabelsDetails.Count%>;
            var hashtable = {};
            var json = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(this.LabelsDetails)%>;
            if (hashTableSize != 'undefined' && hashTableSize > 0)
            {
                for (var key in json)
                { 
                    hashtable[json[key].LabelId] = json[key].LabelDesc;
                }

            }
}
函数设置条形图(JQObjectContainer,JsonData){
var hashTableSize=;
var hashtable={};
var json=;
if(hashTableSize!=“未定义”&&hashTableSize>0)
{
for(json中的var键)
{ 
hashtable[json[key].LabelId]=json[key].LabelDesc;
}
}
}
试试这个

function setupBarChart(JQObjectContainer, JsonData) {
            var hashTableSize = <%=this.LabelsDetails.Count%>;
            var hashtable = {};
            var json = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(this.LabelsDetails)%>;
            if (hashTableSize != 'undefined' && hashTableSize > 0)
            {
                for (var key in json)
                { 
                    hashtable[json[key].LabelId] = json[key].LabelDesc;
                }

            }
}
函数设置条形图(JQObjectContainer,JsonData){
var hashTableSize=;
var hashtable={};
var json=;
if(hashTableSize!=“未定义”&&hashTableSize>0)
{
for(json中的var键)
{ 
hashtable[json[key].LabelId]=json[key].LabelDesc;
}
}
}
试试这个

function setupBarChart(JQObjectContainer, JsonData) {
            var hashTableSize = <%=this.LabelsDetails.Count%>;
            var hashtable = {};
            var json = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(this.LabelsDetails)%>;
            if (hashTableSize != 'undefined' && hashTableSize > 0)
            {
                for (var key in json)
                { 
                    hashtable[json[key].LabelId] = json[key].LabelDesc;
                }

            }
}
函数设置条形图(JQObjectContainer,JsonData){