javascript 将 c# 数组传递给 java 脚本

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

passing c# array to java script

c#javascriptasp.netjson

提问by Inderpal Singh

I have an array in my page_load in c# which i want to access in java script but don't know how to do that..

我在 c# 中的 page_load 中有一个数组,我想在 java 脚本中访问它,但不知道该怎么做..

float [] energyArray = new float[count];
for (int i = 0; i < count; i++)
{
    energyArray[i] = energyObj[i].FwdHr;
}

Now i want to access in javascript in place of data-

现在我想在 javascript 中访问而不是数据-

series: [{
    name: 'Tokyo',
    data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}]       

回答by CodingIntrigue

A very easy way is to use the JavaScriptSerializerclass to transform your C# object into JSON:

一个非常简单的方法是使用JavaScriptSerializer类将您的 C# 对象转换为 JSON:

C#

C#

float [] energyArray = new float[count];
for (int i = 0; i < count; i++)
   {
       energyArray[i] = energyObj[i].FwdHr;
   }

Javascript:

Javascript:

var dataArray = <%=new JavaScriptSerializer().Serialize(energyArray);%>;
var series = [{
            name: 'Tokyo',
            data: dataArray
        }];

回答by LukeHennerley

Changing your problem a little bit here...

在这里稍微改变你的问题......

Instead of manipulating an already existing script, consider constructing the whole javascript string block and then use Page.RegisterClientScriptBlock.

与其操作已经存在的脚本,不如考虑构建整个 javascript 字符串块,然后使用Page.RegisterClientScriptBlock.

http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerclientscriptblock.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerclientscriptblock.aspx

int[] yourArray = new int[] { 1, 2, 3 };
string arrElements = string.Join(",", yourArray.Select(x => x.ToString()).ToArray());
string strJs = string.Format("var yourArray=[{0}]", arrElements);
RegisterClientScriptBlock("Test", strJs);

回答by Igarioshka

you would need to pass the array to the client side (tha javascript part) somehow:

您需要以某种方式将数组传递给客户端(tha javascript 部分):

I would suggest making an ajax request to a page, that would return the serialized array or as @Blade0rz suggested, to output the serialized string directly to the page. to serialize the array to a JSON format you would call the methods of the JavaScriptSerializer class:

我建议向页面发出 ajax 请求,该请求将返回序列化数组或如@Blade0rz 建议的那样,将序列化字符串直接输出到页面。要将数组序列化为 JSON 格式,您将调用 JavaScriptSerializer 类的方法:

more on it here

更多关于这里

回答by Freelancer

C# code behind:

后面的 C# 代码:

float [] energyArray = new float[count];
 public JavaScriptSerializer javaSerial = new JavaScriptSerializer();

Try This Code:

试试这个代码:

<script>
var a = <%= this.javaSerial.Serialize(this.energyArray) %>;
for (var i = 0; i < a.length; i++) {
        console.log(a[i]);
    }
 </script>

回答by Mikey Mouse

Declare a HiddenField

声明一个隐藏字段

    <asp:HiddenField id="myHiddenField" runat="server"

Set it's value to your array.Tostring() in the code behind Then in your javascript

在后面的代码中将其值设置为您的 array.Tostring() Then 在您的 javascript 中

    var h = document.getElementById('myHiddenField');
    //Should give you an array of strings that you can cast to integers