Click here to Skip to main content
15,913,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have two dimensional array in java script. these two dimensional array have to pass server side when submit button is clicked.
Posted

 
Share this answer
 
Javascript array cannot be recognized by C#. So, you need to do a trick to serialize the javascript array to a string so that the string can be deserialized in C#.

You can use your custom serialization to convert an array to a string.
In the example I follow the json string, but you can use any logic.

<script language="javascript">
skillArray = new Array(5)

skillArray [0] = new Array(2)
skillArray [0][0] = "Jon"
skillArray [0][1] = "DOT.NET"

skillArray [1] = new Array(2)
skillArray [1][0] = "Tim"
skillArray [1][1] = "C++"

skillArray [2] = new Array(2)
skillArray [2][0] = "Dan"
skillArray [2][1] = "C"

skillArray [3] = new Array(2)
skillArray [3][0] = "Alex"
skillArray [3][1] = "Java"

skillArray [4] = new Array(2)
skillArray [4][0] = "Robert"
skillArray [4][1] = "Perl"

var s='';
for (i=0;i<skillarray.length;>  for (j=0;j<2; j++) {
     if(j==0)
       s += '"' + skillArray[i][j] + '"' + ':';
     else
       s += '"' + skillArray[i][j] + '"';
  }
  s += ",";
}
alert(s);
</script>


So the string will look like
"Jon":"DOT.NET","Tim":"C++","Dan":"C","Alex":"Java","Robert":"Perl"

User Ajax to send the string to C# (code behind).
User split function to split first the primary substring
"Jon":"DOT.NET"
"Tim":"C++"
"Dan":"C"
"Alex":"Java"
"Robert":"Perl"

Now use a loop to iterate each element and split each element with ":"
Thus you can create a 2 dimensional array in C# same as javascript.

cheers
 
Share this 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