Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Is there a way to send an Array from Default.aspx.cs to Default.aspx into a JavaScript script?

VB
ClientScript.RegisterStartupScript(GetType(), "JavaScript", "JavaScript:function1(x,y,z); ", True)

x,y,z can only be single variable.

Send an array before executing a ClientScript.RegisterStartupScript?

What I have tried:

Tried ClientScript.RegisterStartupScript with single variable. Too long and consuming.
Posted
Updated 21-Apr-16 14:57pm
v2
Comments
Karthik_Mahalingam 21-Apr-16 20:57pm    
what is the issue?
Sergey Alexandrovich Kryukov 21-Apr-16 21:37pm    
The question makes no sense. Before registering a script, without having a script in your file, there is no a script. You probably just don't understand what happens on server side and what on client side.
—SA

1 solution

try this

c#

C#
ClientScript.RegisterStartupScript(GetType(), "JavaScript", "JavaScript:function1([1,2,3,4,5],['one','two','three']); ", true);


Javascript:

JavaScript
function function1(intArray,stringArray) {
            console.log(intArray);
            console.log(stringArray);
        }


for dynamic array[string]:
C#
string[] array = new string[] {"one","two","three","four","five" };
          string arrayString = "[";
          foreach (string item in array)
              arrayString += string.Format("'{0}',", item);
          arrayString.Trim().Trim(',');
          arrayString = arrayString + "]";

          ClientScript.RegisterStartupScript(GetType(), "JavaScript", "JavaScript:function1( " + arrayString + "); ", true);



For Double:
C#
double[] array = new double[] { 1.1,2.2,3.3,4.4,5.5 };
           string arrayString = "[";
           for (int i = 0; i < array.Length; i++)
               arrayString += string.Format("{0},", array[i]);
           arrayString.Trim().Trim(',');
           arrayString = arrayString + "]";
           ClientScript.RegisterStartupScript(GetType(), "JavaScript", "JavaScript:function1( " + arrayString + "); ", true);


another way: javascript - passing c# array to java script[^]
Passing arrayList to javascript function[^]
 
Share this answer
 
v3
Comments
Member 11242200 21-Apr-16 21:13pm    
Page.ClientScript.RegisterStartupScript(Me.GetType(), "JavaScript", "JavaScript:natalchart(" + array1 + "); ", True)
does not work is there a qualifier for array1(5)?
Karthik_Mahalingam 21-Apr-16 21:19pm    
Is that a string array?
Member 11242200 21-Apr-16 21:22pm    
NoDim array1(5) As Double in vb or
double array1 = new double[5] in C#
Karthik_Mahalingam 21-Apr-16 21:37pm    
it should be var array1 = new double[5] in C#
check my updated answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900