4

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..

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-

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]
}]       
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
Inderpal Singh
  • 270
  • 2
  • 8
  • 24
  • You could store the series as a string in a HiddenField then split it up in the javascript – Mikey Mouse May 30 '13 at 10:21
  • you might using it for displaying high chart i guess, i normally use this in .cs page.... Page.ClientScript.RegisterStartupScript(typeof(Page), "abc", stringARRAY ); – Hisham Jun 13 '13 at 13:32

5 Answers5

8

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

C#

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

Javascript:

var dataArray = <%=new JavaScriptSerializer().Serialize(energyArray);%>;
var series = [{
            name: 'Tokyo',
            data: dataArray
        }];
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
3

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.

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);
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
LukeHennerley
  • 6,344
  • 1
  • 32
  • 50
0

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

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:

more on it here

Igarioshka
  • 677
  • 3
  • 14
0

C# code behind:

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>
Freelancer
  • 9,008
  • 7
  • 42
  • 81
  • `javaSerial` is not a C# function – Hogan May 30 '13 at 10:27
  • Because `javaSerial` is not a C# function and you are calling it in C# -- this won't compile. – Hogan May 30 '13 at 10:32
  • @Hogan Refer this question and answer : http://stackoverflow.com/questions/14942385/pass-c-sharp-array-to-javascript – Freelancer May 30 '13 at 10:34
  • 2
    That code has nothing to do with this answer, if you want to include all the code there then it might be a correct answer, but as it stands this answer is wrong and gets -1. Fix it and I will remove the downvote. – Hogan May 30 '13 at 10:36
0

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

    var h = document.getElementById('myHiddenField');
    //Should give you an array of strings that you can cast to integers 
Mikey Mouse
  • 2,968
  • 2
  • 26
  • 44