Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

70
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda