-2

I need to run a Javascript code within my ASP.NET project. In my ASP.NET project I have an array of information and i need to send it to javaScript to get handle information. Does anyone know how to solve this problem?

My code is this:

Asp.net ARRAY´s:

foreach (string music in distinctMusic)
{
    musicList[i] = music;
    i++;
}

foreach (int musicDuration in distinctMusicDuration)
{
    musicDurationList[k] = musicDuration;
    k++;
}

JavaScript function:

function playNext(listMusic, musicDuration) {
    alert(listMusic[1]); //Test ...
}

That does not work. Help?

Imad
  • 7,126
  • 12
  • 55
  • 112
Flávio Costa
  • 895
  • 5
  • 18
  • 41

3 Answers3

0

You could serialize the array to JSON and then de-serialize within Javascript. I think that will be the cleaner solution.

ASP .NET JSON SERIALIZER

Check this question from JSON to javascript array.

Community
  • 1
  • 1
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
0

Use ClientScriptManager.RegisterArrayDeclaration Method. It allows you to declare array name and array value (as comma-separated string)

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
0

You'll have to setup a property in your code behind and then you can access that property from your js.

public string[] musicList;

function playNext(listMusic, musicDuration) {
var musicList = <%=this.musicList%>;
}

I haven't tested this as I'm not at a good place to test it, but it should work in theory.

Kevin DeVoe
  • 600
  • 2
  • 8