473,732 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass ASP Array variable to Javascript

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...
Nov 17 '05 #1
2 6444
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...

Nov 17 '05 #2
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...



Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5954
by: Miguel | last post by:
Hello. I have a Javascript function that validates an specific form. As parameters this function receives an array of elements to be checked. Depending on the form in cause, the array could have variable lenght. I don't know how can i pass a dynamic array on 'onsubmit' event form PHP code to a JavaScript function. I would like to do something like: <script>
3
6790
by: Nath | last post by:
Please help!? I am new to writing html, javascript, pretty new to MySQL but quite proficient at writing Perl and i'm a quick learner. I am building a database driven website and i am a little stuck: I have page of results obtained from a MySQL query presented as a table (the first column having checkboxes for each of the rows in the table, and all having the name "seqs"). I have set up a javascript (connected to a "toggle all" checkbox)...
5
3150
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for (count = 0; count <= 1; count++) { if (eval(f.RadioButtons.checked)) { btnChosen = true; }
5
3421
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? /******************************************************** Below is my code: ********************************************************/
3
1681
by: Boki | last post by:
Hi, Could you please advice, can javascript accept this kind of code? function resone(cnt) { alert("document.all.txt_note"+cnt) // cnt is the textbox index
2
1632
by: André | last post by:
Hi, I have to pass a lot of variables from vb.net to javascript using client callback. the problem is that there are single variables, but also arrays with differents indexes. I have two questions: 1) is this technology made for passing a lot of variables or in fact only for one? 2) does it exist a esier way to pass the values and to recover them in javascript?
4
4339
by: IRC | last post by:
hey, i am pretty new on javascript as well as PHP, Hey, anyone can you help me, how to pass the javascript array value to php page......... i want to retrieve the values which are arrayed on "selectedValues" from next page for php variable this is my javascript code saved on "sendValue.js" file, <script> function updateRecordEntry(requestValue){
11
3360
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
4
2377
by: akshay01 | last post by:
Hi All, I am using the following code in which i am creating some textboxes and and as the for loop continues the name of the textboxes will also be unique for all the textboxes. now i want to store the textbox values into some array and want to pass the array as an value of hidden variable <% int i =0; String arr_TextValues = new String; String arr_ChkValues = new String;
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.