如何在asp.net中从javascript访问c数组

本文关键字:javascript 访问 数组 中从 net asp | 更新日期: 2023-09-27 18:26:16

我有一个ASP.NET web窗体web应用程序我在codebehind中定义了一个数组,我想通过javascript 在我的页面中访问它

public int[] GetSomeArray()
{
    int[] Labels;
    Labels = new int[8];
    Labels[0] = 100;
    Labels[1] = 90;
    Labels[2] = 111;
    Labels[3] = 121;
    Labels[4] = 81;
    Labels[5] = 102;
    Labels[6] = 93;
    Labels[7] = 103;
    return Labels;
}

如何在javascript代码中访问这些值?

如何在asp.net中从javascript访问c数组

修改函数如下

    public string GetSomeArray()
        {
            int[] Labels;
            Labels = new int[8];
            Labels[0] = 100;
            Labels[1] = 90;
            Labels[2] = 111;
            Labels[3] = 121;
            Labels[4] = 81;
            Labels[5] = 102;
            Labels[6] = 93;
            Labels[7] = 103;
            JavaScriptSerializer j = new JavaScriptSerializer();
            return j.Serialize(Labels);
}

并在页面上添加

<script type="text/javascript">
         var array = <%= GetSomeArray() %>;
</script>