faqts : Computers : Programming : Languages : Asp : ASP/VBScript : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 21 people (38%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How can I put server side variables into a client side script? An ASP generated array into the likes of JavaScript and DHTML.

Jul 13th, 2000 22:12
unknown unknown, Bruce Anwyl


The following shows how to embed a single variable value from server 
code and a technique for creating an array with 3 columns. It is used to 
populate a dynamic list box based on a selection made from another 
without making trips to the server.
It is only an extract but I hope it helps.
<script language="javascript">
//      Declaring Debug variable
var javaDebug = "<%=bDebug%>";
var thisDoc;
//  Create an array of Job Categories
var arrJobCategory = new Array;
//  Create a Job Category element
function JobCategory(jobGroupID, jobCategoryID, jobCategoryName)
{
        this.jobGroupID = jobGroupID;
        this.jobCategoryID = jobCategoryID;
        this.jobCategoryName = jobCategoryName;
}
//
//  Update the Job Categories in the Select object
//  based on the current Job Group.
//
function refreshJobCategories()
{
        var option;
        var jobCategoryID;
        var j;
        var i;
        i=0;
        //
        //  Clear the drop down list of it's current values
        //
        while (frmCategoryReport.cboJobCategory.length != 0)
        {
                frmCategoryReport.cboJobCategory.remove(0);
        }
        //
        //Get the Job Group ID
        //
        jobGroupID = frmCategoryReport.cboJobGroup.value;
        //Add a blank field
        /*
        option = document.createElement("OPTION");
        option.value = 0;
        option.text = '';
        document.forms(0).cboJobCategory.add(option);
        */
        for (i = 0; i < arrJobCategory.length; i++)
        {
                if (arrJobCategory[i].jobGroupID==jobGroupID)
                {
                        option = document.createElement("OPTION");
                        option.value = arrJobCategory[i].jobCategoryID;
                        option.text = arrJobCategory[i].jobCategoryName;
                        frmCategoryReport.cboJobCategory.add(option);
                }
        }
        //
        //  Make the first item in the list the default selection.
        //
        frmCategoryReport.cboJobCategory(0).selected = true;
        frmCategoryReport.hdnJobCategory.value =
frmCategoryReport.cboJobCategory(0).outerText
}
//
//  Fill in the Block array
//
<%
        If Not rstJobCat.Eof Then
                i = 0
                While Not rstJobCat.EOF
                        Response.write "arrJobCategory[" & i & "] = new 
JobCategory(" &
rstJobCat(0) & ", " & rstJobCat(1) & ", '" & rstJobCat(2) & "')" & 
vbCrLf
                        i = i + 1
                        rstJobCat.MoveNext
                Wend
        End If
        rstJobCat.Close
        set rstJobCat = Nothing
%>



© 1999-2004 Synop Pty Ltd