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.EventArg s) Handles MyBase.Load
Dim Fruit() As String = {"Apples", "Bananas", "Grapes", "Pears"}
Dim s As New System.Text.Str ingBuilder
s.Append("<scri pt>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>")
RegisterClientS criptBlock("Fru it", s.ToString)
s = New System.Text.Str ingBuilder
s.Append("<scri pt>var i; var s = '';")
s.Append("for (i = 0; i < arr.length; i++)")
s.Append("{s = s + arr[i] + ' '}")
s.Append(" document.all('T extBox1').value = s;</script>")
RegisterStartup Script("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" <chrisjATmvpsDO TorgNOSPAM>
References: <CE************ *************** *******@microso ft.com>
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: <eT************ **@tk2msftngp13 .phx.gbl>
Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
NNTP-Posting-Host: 208.252.52.2
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1891 10
X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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" <au*****@semi.c om.br> wrote in message
news:CE******** *************** ***********@mic rosoft.com... Hello people.
How can I Pass ASP Array variable to Javascript;
I´m to trying this:
<script language="JavaS cript">
var x = new Array(10);
x = <%= myArrayAsp %>;
</script>
but return error to me...