Pass ASP Array variable to Javascript

G

Guest

Hello people.

How can I Pass ASP Array variable to Javascript;

I´m to trying this:

<script language="JavaScript">

var x = new Array(10);

x = <%= myArrayAsp %>;

</script>

but return error to me...
 
C

Chris Jackson

What ASP.NET sends to the browser is plain text, not objects. You need to
keep in mind that you have two different programming models - one on the
server, and one on the client. If you aren't writing the text for the client
side on the server-side code, then the client isn't getting it. The code you
display here assumes that the client can, in some way, communicate with the
server and get that variable, and that this variable will be type
compatible. Neither assumption is true.

Instead, you need your server side code to write out the code to initialize
this array. Instead of just databinding to the array type, you'll have to
iterate through the array and write it all out:

(my vbscript skills are very rusty, so don't type this in directly, but you
get the idea...)

var x = new Array(10);
<%
dim i
for i = 0 to Len(myArrayAsp) - 1
' Assumes 0-based on client and 1-based on server
Response.Write "x[" & i & "] = " & myArrayAsp(i + 1) & ";"
next i
%>
 
M

Mike Moore [MSFT]

Hi,

Chris is totally right. I started working on this before I saw Chris' post.
He's got the solution for you. My solution is just a variation on his. All
mine adds is how to run through the array after it's on the client. It also
uses a couple different techniques.

* place a text box on an ASPX page
* Add the following to the code-behind
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim Fruit() As String = {"Apples", "Bananas", "Grapes", "Pears"}
Dim s As New System.Text.StringBuilder
s.Append("<script>var arr = new Array(")
Dim i As Integer
For i = 0 To Fruit.Length - 1
s.Append("'" & Fruit(i) & "'")
If i < (Fruit.Length - 1) Then
s.Append(",")
End If
Next
s.Append(");</script>")
RegisterClientScriptBlock("Fruit", s.ToString)

s = New System.Text.StringBuilder
s.Append("<script>var i; var s = '';")
s.Append("for (i = 0; i < arr.length; i++)")
s.Append("{s = s + arr + ' '}")
s.Append(" document.all('TextBox1').value = s;</script>")
RegisterStartupScript("Show", s.ToString)
End Sub


Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
From: "Chris Jackson" <chrisjATmvpsDOTorgNOSPAM>
References: <[email protected]>
Subject: Re: Pass ASP Array variable to Javascript
Date: Thu, 6 Nov 2003 15:58:41 -0500
Lines: 55
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 208.252.52.2
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:189110
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

What ASP.NET sends to the browser is plain text, not objects. You need to
keep in mind that you have two different programming models - one on the
server, and one on the client. If you aren't writing the text for the client
side on the server-side code, then the client isn't getting it. The code you
display here assumes that the client can, in some way, communicate with the
server and get that variable, and that this variable will be type
compatible. Neither assumption is true.

Instead, you need your server side code to write out the code to initialize
this array. Instead of just databinding to the array type, you'll have to
iterate through the array and write it all out:

(my vbscript skills are very rusty, so don't type this in directly, but you
get the idea...)

var x = new Array(10);
<%
dim i
for i = 0 to Len(myArrayAsp) - 1
' Assumes 0-based on client and 1-based on server
Response.Write "x[" & i & "] = " & myArrayAsp(i + 1) & ";"
next i
%>


--
Chris Jackson
Software Engineer
Microsoft MVP - Windows XP
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

Augusto Cesar said:
Hello people.

How can I Pass ASP Array variable to Javascript;

I´m to trying this:

<script language="JavaScript">

var x = new Array(10);

x = <%= myArrayAsp %>;

</script>

but return error to me...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top