Bytes Expert Newtork: Connect with experts in IT / Business | Expert Topics



Search


Go Back   Programmers Resource > Code Section > ASP Articles
User Name
Password


 
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 06-12-2003, 12:01 PM
Angelika Angelika is offline
Moderator
 
Join Date: May 2003
Location: NJ
Posts: 125
Converting VBScript Array to Javascript Array in the same ASP Page

We come across lot of times a need to convert an Array from ASP page to Javscript, so that we can manipulate in the HTML form using Javascript. Here is an example of how to do that. I have two include files, one for 1D array, one for 2D array and one example page calling those files. You can modify the array, ie calling from a Database (result set) or a COM Component (VB Array) and have the array in Javascript and populate HTML forms and manipulate. Some documentation is available in the code itself. Happy programming!!!


This is the calling page code

<!-- #INCLUDE FILE = "VB2JSarray2D.inc" -->
<!-- #INCLUDE FILE = "VB2JSarray1D.inc" -->
<HTML>
<HEAD>
<!--What ever U wanna write -->
</HEAD>
<BODY>
<!--What ever U wanna write -->
<%
Dim arrUserInfo1D(4)

arrUserInfo1D(0) = "Name1"
arrUserInfo1D(1) = "Name2"
arrUserInfo1D(2) = "Name3"
arrUserInfo1D(3) = "Name4"
arrUserInfo1D(4) = "Name5"

Call ConvertToJSArray1D(arrUserInfo1D,"arrUserInfo1D")
Response.Write("<H2>One Dimensional Array</H2>")
%>

<SCRIPT LANGUAGE="JAVASCRIPT">

document.write("<table border=1>");
for (i=0; i < arrUserInfo1D.length; i++)
{
document.write("<tr>");
document.write("<td>");
document.write(arrUserInfo1D[i]);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");

</SCRIPT>

<%

Dim arrUserInfo2D(2,4)

arrUserInfo2D(0,0) = "FirstName1"
arrUserInfo2D(0,1) = "FirstName2"
arrUserInfo2D(0,2) = "FirstName3"
arrUserInfo2D(0,3) = "FirstName4"
arrUserInfo2D(0,4) = "FirstName5"

arrUserInfo2D(1,0) = "LastName1"
arrUserInfo2D(1,1) = "LastName2"
arrUserInfo2D(1,2) = "LastName3"
arrUserInfo2D(1,3) = "LastName4"
arrUserInfo2D(1,4) = "LastName5"

arrUserInfo2D(2,0) = "Age1"
arrUserInfo2D(2,1) = "Age2"
arrUserInfo2D(2,2) = "Age3"
arrUserInfo2D(2,3) = "Age4"
arrUserInfo2D(2,4) = "Age5"

Call ConvertToJSArray2D(arrUserInfo2D,"arrUserInfo2D")
Response.Write("<H2>Two Dimensional Array</H2>")

%>

<SCRIPT LANGUAGE="JAVASCRIPT">

document.write("<TABLE BORDER='1'>");
for (i=0; i < arrUserInfo2D.length; i++)
{
document.write("<TR>");
for (j=0; j < arrUserInfo2D[i].length; j++)
{
document.write("<TD>");
document.write(arrUserInfo2D[i][j]);
document.write("</TD>");
}
document.write("</TR>");
}
document.write("</TABLE>");

</SCRIPT>
<!--What ever U wanna write -->
</BODY>
</HTML>

1st Include File

<%
'************************************************* *********************************
'Name : VB2JSarray1D.inc
'Description : Include File used to convert a VBScript One Dimensional Array to One Dimensional Javascript Array
'Developer : Anand Venkatraman
'Creation Date : 03/31/2000
'************************************************* **********************************

'This function converts a VBScript Array to Javascript Array(One Dimensional array only)
'Inputs are VBScript array and Javascript array name
'For the sake of variable names "mismatch", "vb2js" is prefixed to all the variables
'For eg. in ASP page you might call this function as "Call ConvertToJSArray(arrUserList,"arrUserList")
'When this function is called immediately Javascript array is created and written to the web page
'So we cannot use Response.Redirect after calling this function. You can View the Source of HTML page
'to see the Javascript array.

Function ConvertToJSArray1D(VBArray , ArrayName)

Dim vb2jsRow , vb2jsStr, vb2jsi
vb2jsRow = Ubound(VBArray,1)
%>
<SCRIPT LANGUAGE = 'JAVASCRIPT' >
var vb2jsi
<%=ArrayName%> = new Array(<%=vb2jsRow+1%>);
for (vb2jsi=0; vb2jsi < <%=vb2jsRow+1%>; vb2jsi++)
{
<%=ArrayName%>[vb2jsi]= " "
}
</SCRIPT>
<%
Response.Write("<SCR"&"IPT LANGUAGE = 'JAVASCRIPT' >"&chr(13))
for vb2jsi=0 to vb2jsRow
vb2jsstr = "VBArray("&vb2jsi&")"
%>
<%=ArrayName%>[<%=vb2jsi%>]= "<%=trim(eval(vb2jsstr))%>"
<%
Next
Response.Write("</SCR"&"IPT>")
End Function
%>


Second Include File

<%
'************************************************* *********************************
'Name : VB2JSarray2D.inc
'Description : Include File used to convert a VBScript Two Dimensional Array to Two Dimensional Javascript Array
'Developer : Anand Venkatraman
'Creation Date : 03/31/2000
'************************************************* **********************************

'This function converts a VBScript Array to Javascript Array(Two Dimensional array only)
'Inputs are VBScript array and Javascript array name
'For the sake of variable names "mismatch", "vb2js" is prefixed to all the variables
'For eg. in ASP page you might call this function as "Call ConvertToJSArray(arrUserList,"arrUserList")
'When this function is called immediately Javascript array is created and written to the web page
'So we cannot use Response.Redirect after calling this function. You can View the Source of HTML page
'to see the Javascript array.

Function ConvertToJSArray2D(VBArray , ArrayName)

Dim vb2jsRow, vb2jsCol , vb2jsStr, vb2jsi, vb2jsj
vb2jsRow = Ubound(VBArray,1)
vb2jsCol = Ubound(VBArray,2)
%>
<SCRIPT LANGUAGE = 'JAVASCRIPT' >
var vb2jsi,vb2jsj
<%=ArrayName%> = new Array(<%=vb2jsRow+1%>);
for (vb2jsi=0; vb2jsi < <%=vb2jsRow+1%>; vb2jsi++)
{
<%=ArrayName%>[vb2jsi] = new Array(<%=vb2jsCol+1%>)
for (vb2jsj=0; vb2jsj < <%=vb2jsCol+1%>; vb2jsj++) <%=ArrayName%>[vb2jsi][vb2jsj] = " "
}
</SCRIPT>
<%
Response.Write("<SCR"&"IPT LANGUAGE = 'JAVASCRIPT' >"&chr(13))
for vb2jsi=0 to vb2jsRow
for vb2jsj=0 to vb2jsCol
vb2jsstr = "VBArray("&vb2jsi&","&vb2jsj&")"
%>
<%=ArrayName%>[<%=vb2jsi%>][<%=vb2jsj%>] = "<%=trim(eval(vb2jsstr))%>"
<%
Next
Next
Response.Write("</SCR"&"IPT>")
End Function

%>

http://www.aspfree.com
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT -5. The time now is 03:12 AM.



Powered by: vBulletin Version 3.0.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All content Copyright ©1999 - 2010, Programmers Resource, unless otherwise noted.