Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

71
Vistas
Trying to pass an array variable from asp to javascript

trying to pass an array variable to java The following is not working, any idea why and how to make it work

Code behind

Public myArray (5) As String
myArray(1) = "A1"
myArray(2) = "A2"
myArray(3) = "A3"
myArray(4) = "A4"
myArray(5) = "A5"

In asp

<button type = "button" onclick="myJava('<%= myArray %>');">Search</button>

In Javascript

function myJava (myArray) {
   alert(myArray[1]); // expected answer is A1 but it is not
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to properly encode your array. Firstly, you need to convert it to a JSON string; then you need to encode that string as an HTML attribute value.

Add a reference to the Newtonsoft.Json NuGet package. Add an Imports statement for the Newtonsoft.Json namespace. Add a method to your code-behind to return the properly-encoded function call:

Public Function PassArrayToJavascript(ByVal functionName As String) As String
    Dim json As String = JsonConvert.SerializeObject(myArray)
    Dim result As String = functionName + "(" + json + ");"
    Return System.Web.HttpUtility.HtmlAttributeEncode(result)
End Function

Then update your markup to call this function:

<button type = "button" onclick="<%= PassArrayToJavascript("myFunction") %>">Search</button>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda