Solved

Arrays

Posted on 2001-08-27
12
729 Views
Last Modified: 2008-03-03
I am trying to get data out of an array using a client-side subroutine in a ASP page.  I have tried to access the content of the array 2 different ways. The first way I defined the array with no dimensions between the script tags, which I thought meant the array was accessable by the entire page. This gives me a subscript out of range error when the subroutine is called from the onclick. I then tried defining the array with dimensions and removing the redim.  When I do that I get an "op_array not defined" error when reading the data into the array.

What am I doing wrong?

This is the current code


<SCRIPT LANGUAGE=vbscript>
<!--
Dim op_array()
'Dim op_array(500,11)

sub set_plus()
     dim num
     num = mid(window.event.srcElement.id,4,len
              (window.event.srcElement.id))
     msgbox op_array(num,8)
end sub
</script>

<body>
<%
get the # records from the database and set it equal to "count"

redim op_array(count,11)

select the fields needed for the array into a recordset
loop thru the recordset and insert data into array using counter - i=0

op_array(i,0) = recordset.field
  .
  .
  .
  .

I display data to the screen in a table using the array while looping thru the recordset.

Response.Write "<td><input type=button name=Red" & i & " id=Red" & i & " value=R onclick=set_plus()>"

i=i+1
next record
loop

%>
</body>
0
Comment
Question by:agomen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6429512
1) vbscript, by default, ONLY runs on the SERVER, not the client.  IF you want the VBScript to run on the CLient, add the paramenter RUNAT=CLIENT:

<SCRIPT LANGUAGE=vbscript RUNAT=CLient>

but you are then limiting the use of the page to IE4 or later

2)

Dim op_Array()

defines an array with no fixed dimension...before you can actually use it you MUST use the ReDim statemetn to assign the actual dimension.  This will allow you to dynamically resize the array at run-time to allow it to grow as more elements are added.  (With a 2-dimensioned array, you can only dynamically resize the second dimension).  If you wish to keep any existing values in the array when you ReDim, use th additional keyword Preserve


dim op_array()

ReDim Preserve op_array(10) ' this will clear all exiting values, and allow you to enter 10 values into the array
.
.
.
ReDim Preserve op_array(20)   ' this will keep the first 10 values, and aloow you to add 10 more (for a total of 20)

Lookup the ReDim command in the VB Help System.
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 200 total points
ID: 6429640
Nope, Arthur, scripts always run at the client, unless you state that it should run at the server.

The <% %> pairs is actually equivalent to:
<script language="vbscript" runat="server"> </script>

The problem here is that the array created on the server side does not exist on the client side. First the script is run on the server, then the page is sent to the browser, and the script is run on the client.

To send the data to the client you have to do something like:

<%
' get data from db here...
strArr=""
While not recordset.EOF
   strArr=strAdd & "," & recordset("field")
   recordset.MoveNext
Wend
%>
<script language="vbscript">
op_array=Array(<%=Mid(strArr,2)%>);
</script>

This will create an array on the client. If you need a two dimensional array, you will have to create a script that looks like:

op_array=Array(Array(...),Array(...),Array(...))
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6431593
GreenGhost...is what you say about <%...%> is true, then WHY is it that ALL (every one, without exception) of my ASP pages with <%....%> embedded BSScript has that Script Executing ON THE SERVER...and only those pages that include JScript has any code that executes on the CLient, and then it is only the JavaScript that executes on the Client?  AM I just lucky???
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 29

Expert Comment

by:Göran Andersson
ID: 6431696
Arthur, cause I said so.

To clarify:

<script language="javascript">
//this runs at the client
</script>

<script language="vbscript">
'this runs at the client
</script>

<script language="vbscript" runat="server">
'this runs at the server last
</script>

<%
'this runs at the server before the one above
%>

<script language="javascript" runat="server">
//this runs at the server before any vbscript
</script>
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6431738
Sorry GreenGhost, but I have NEVER EVER EVER needed to explicitly specify RUNAT=SERVER for my VBScript.  I guess I am just lucky that ALL ob my VBScript spontaneously "knows" that it is supposed to run on my server.  How am I so lucky?

By the way, I believe that RUNAT=CLIENT WAS the default for ASP 2.0, and RUNAT=SERVER IS the default for ASP 3.0 and later
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6432134
to GreenGhost:

this is from the Visual Interdev Help for RUNAT:

"When you write server script in an .asp file, you distinguish it from other text (including client script) in one of two ways:

Within the delimiters <% and %>. Any text between these two tags is processed as INLINE SERVER SCRIPT by IIS[My emphasis]. The <% %> delimiters are often used to enclose expressions that are evaluated and inserted into the HTML text of a page. For example, the following server script displays the current time on a page:
<% response.write time %>

In a <SCRIPT> tag (as with client scripts), but with the RUNAT=SERVER property, which is used to enclose stand-alone procedures such as functions and subroutines. The following example shows the RUNAT property:
<SCRIPT RUNAT=SERVER>
   Function GetDate
      [some script lines here]
   End Function
</SCRIPT>

In the Visual InterDev HTML editor, server script appears in yellow to distinguish it from client script. The following illustration shows a page that includes both server script (bracketed in yellow) and HTML text."

Note the distinction between "<%...%>" and "<SCRIPT>...</SCRIPT>"

0
 

Author Comment

by:agomen
ID: 6432464
Thanks for the help
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 6432566
I don't get you, Arthur.

First you write "I have NEVER EVER EVER needed to explicitly specify RUNAT=SERVER for my VBScript".

If I understand that correct, you use:

<script language="VBScript">
</script>

and it runs on the server? Otherwise, you don't have a point here. And in that case, your IIS does not have the default settings.

Then you quoute the Interdev manual, that says precisely what I said.

Runat=client is still the default in asp 3 BTW.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6432704
I DO NOT EVER USE <Script language="VBScript">...</SCRIPT>


I Alswys use <%....%> for Server-side

or <SCRIPT LANGUAGER="JavaScript">...</SCRIPT> for Client-Side

0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 6432734
Then what's the fuzz about?

Read my first posting again, and see if you get it. ;)
0
 

Author Comment

by:agomen
ID: 6433250
This is what I have done. When the page starts loading I get a type mismatch on myarray=Array(<%=Mid(strArr,2)%>)
Can you please help, this is my first time doing this.


<SCRIPT LANGUAGE=vbscript>
<!--
dim myarray()

sub set_plus()
     dim num
     num = mid(window.event.srcElement.id,4,len
            (window.event.srcElement.id))
     msgbox myarray(num)
end sub
-->
</SCRIPT>

</HEAD>
<BODY>

<%
'get count of records from database
redim myarray(cnt)

'get records from database
while not rs.EOF
     strArr = strAdd&","&rs.Fields("field")
     rs.MoveNext
Wend
%>
<SCRIPT LANGUAGE=vbscript>
<!--
     myarray=Array(<%=Mid(strArr,2)%>)
-->
</SCRIPT>
<%

Response.Write "<td><input type=button name=add" & h & " id=add" & h & " value=+ onclick=set_plus()>"

%>
</body>
0
 

Author Comment

by:agomen
ID: 6433251
This is what I have done. When the page starts loading I get a type mismatch on myarray=Array(<%=Mid(strArr,2)%>)
Can you please help, this is my first time doing this.


<SCRIPT LANGUAGE=vbscript>
<!--
dim myarray()

sub set_plus()
     dim num
     num = mid(window.event.srcElement.id,4,len
            (window.event.srcElement.id))
     msgbox myarray(num)
end sub
-->
</SCRIPT>

</HEAD>
<BODY>

<%
'get count of records from database
redim myarray(cnt)

'get records from database
while not rs.EOF
     strArr = strAdd&","&rs.Fields("field")
     rs.MoveNext
Wend
%>
<SCRIPT LANGUAGE=vbscript>
<!--
     myarray=Array(<%=Mid(strArr,2)%>)
-->
</SCRIPT>
<%

Response.Write "<td><input type=button name=add" & h & " id=add" & h & " value=+ onclick=set_plus()>"

%>
</body>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question