passing c# array to java script

14,364

Solution 1

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
        }];

Solution 2

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);
Share:
14,364
Inderpal Singh
Author by

Inderpal Singh

I am a computer science graduate working with Nagarro CoE IoT as Associate Lead (Technology). I have worked on various development &amp; research projects for more than 5 years which ranges from IoT systems, home automation and web programming. I like to work on new technologies and learn fast.

Updated on June 08, 2022

Comments

  • Inderpal Singh
    Inderpal Singh almost 2 years

    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]
    }]       
    
  • Hogan
    Hogan almost 11 years
    javaSerial is not a C# function
  • Hogan
    Hogan almost 11 years
    Because javaSerial is not a C# function and you are calling it in C# -- this won't compile.
  • Freelancer
    Freelancer almost 11 years
    @Hogan Refer this question and answer : stackoverflow.com/questions/14942385/…
  • Hogan
    Hogan almost 11 years
    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.
  • Inderpal Singh
    Inderpal Singh almost 11 years
    where is javascript serializer.. to import it?? @Blade0rz
  • CodingIntrigue
    CodingIntrigue almost 11 years
    From the linked MSDN page: System.Web.Extensions (in System.Web.Extensions.dll)
  • Inderpal Singh
    Inderpal Singh almost 11 years
    not getting it.. do you know where to change target framework in vs2010?
  • CodingIntrigue
    CodingIntrigue almost 11 years
  • Inderpal Singh
    Inderpal Singh almost 11 years
    thanks but what to do about error - javascriptSerializer not found? should i include it in javascript but how?
  • CodingIntrigue
    CodingIntrigue almost 11 years
    You need to include the System.Web.Extensions dll in your project references as per this article: msdn.microsoft.com/en-us/library/f3st0d45(v=vs.100).aspx
  • Inderpal Singh
    Inderpal Singh almost 11 years