1

I'm building some Web Application using aspx and C# and i want to create an 2D String array in the java script but to initialize it with an array from my c# code. I used the "<% ... %> for it but it didn't work. My code is like this:

iconColors = new String[,];
protected void Page_Load(object sender, EventArgs e)
    { // some code that filling the array
    }  

in Java Script:

<script>
var seatColor = "<%= iconColors %>"; 
for (.... i and j....) {// its a double loop
 document.write("....."+iconColos[i,j]+"...");
}
</script>

again, it doesn't work. help somebody?

4
  • Are you saying your seatColor array is empty or the loop isn't working? Ay console errors? Commented May 12, 2013 at 11:17
  • The loop is not empty. the loop works and but it just doesn't write the array content... In the console it says its unidentified and the error is 500 - Internal server error Commented May 12, 2013 at 11:33
  • You can better serialize the 2D Array to JSON & use the JSON in your javascript. Commented May 12, 2013 at 11:45
  • Can you show me an example? Commented May 12, 2013 at 11:47

2 Answers 2

0

i guess this must work.

<script>
var seatColor = "<%= iconColors %>"; 
for (.... i and j....) {// its a double loop
  document.write("....."+seatColor [i][j]+"...");
}
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

@user1857251 You have iconColors, not seatColor in your loop.
0

There are no native 2D arrays in JavaScript. You need to create your own. I saw someone use this method: var arr = [[1,2],[3,4]]; (an array of arrays)

You'll have to adjust your server code to something that can serialize to that. An array of arrays probably work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.