Connecting Tech Pros Worldwide Forums | Help | Site Map

Pass ASP Array variable to Javascript

Augusto Cesar
Guest
 
Posts: n/a
#1: Nov 17 '05
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...

Chris Jackson
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Pass ASP Array variable to Javascript


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" <augusto@semi.com.br> wrote in message
news:CE74DDD8-C7E2-45B8-8AF0-8BE0BD890608@microsoft.com...[color=blue]
> 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...[/color]


Mike Moore [MSFT]
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Pass ASP Array variable to Javascript


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[i] + ' '}")
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.


--------------------[color=blue]
> From: "Chris Jackson" <chrisjATmvpsDOTorgNOSPAM>
> References: <CE74DDD8-C7E2-45B8-8AF0-8BE0BD890608@microsoft.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: <eTBpDjKpDHA.2536@tk2msftngp13.phx.gbl>
> Newsgroups: microsoft.public.dotnet.framework.aspnet
> NNTP-Posting-Host: 208.252.52.2
> Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.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[/color]
client[color=blue]
> side on the server-side code, then the client isn't getting it. The code[/color]
you[color=blue]
> display here assumes that the client can, in some way, communicate with[/color]
the[color=blue]
> 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[/color]
initialize[color=blue]
> 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[/color]
you[color=blue]
> 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" <augusto@semi.com.br> wrote in message
> news:CE74DDD8-C7E2-45B8-8AF0-8BE0BD890608@microsoft.com...[color=green]
> > 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...[/color]
>
>
>[/color]

Closed Thread