Search code examples
javascriptasp.netarraysvb.netclientscript

passing VB.net array to javascript using ClientScript class


I am trying to use the ASP.net class ClientScript to pass an array to my aspx page. I have succesfully completed an earlier sample to do this (code sample below). But a new routine is not working. The difference is the ClientScript class.

Error states that "routeCoords is undefined"

my java code is...

function newTest() {
    var myArray = [ , ];
    var n = 0;

    var recCount = routeCoords.length / 15;

    for (var i = 0; i < recCount ; i++) {
        for (var s = 0; s < 15; s++) {

            myArray[i, s] = routeCoords[n];
            n++;

            alert(myArray[s], [i]);
        }
    }
}

vb.net to build array and register the script....

 ' arrylist
 For p = 0 To arryLst.Count - 1
     Page.ClientScript.RegisterArrayDeclaration("routeCoords", arryLst(p))
 Next

Dim strScript As String = "newTest();"
ClientScript.RegisterStartupScript(GetType(Page), "newTest", strScript.ToString, True)

the array is correctly populating in vb.net

this is the routine from the sample that is working...

VB.net code:

For s = 0 To arryLst.Count - 1
    Page.ClientScript.RegisterArrayDeclaration("parmTypeAry", arryLst(s))
Next

JAVA code:

  // Create and Element Object of type "option"
  var opt = document.createElement("option");
  //Add the option element to the select item
  listID.options.add(opt);
  //Reading Element From Array
  opt.text = parmTypeAry[s];

which populates a dropdownlist box.


Solution

  • Here is the solution. It required an IF condition in the for loop to check for the array.

          for (i = 0; i < recCount; i++) {
          // Array of arrays builds out each record.
              if (!myArray[i])
                  myArray[i] = []
    
              for (s = 0; s < 16; s++) {
                  //  myArray[i] = new Array(14);
    
                  myArray[i][s] = routeCoordsAry[n];
                  n++;
    
                  //  alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
    
              }
              // var u = 4;
              s = 0;
    
          }