Arrays

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>
agomenAsked:
Who is Participating?
 
Göran AnderssonConnect With a Mentor Commented:
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
 
Arthur_WoodCommented:
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
 
Arthur_WoodCommented:
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Göran AnderssonCommented:
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
 
Arthur_WoodCommented:
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
 
Arthur_WoodCommented:
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
 
agomenAuthor Commented:
Thanks for the help
0
 
Göran AnderssonCommented:
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
 
Arthur_WoodCommented:
I DO NOT EVER USE <Script language="VBScript">...</SCRIPT>


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

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

0
 
Göran AnderssonCommented:
Then what's the fuzz about?

Read my first posting again, and see if you get it. ;)
0
 
agomenAuthor Commented:
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
 
agomenAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.