Dependent combo in classic ASP and Ajax?

Please can you provide me with an example in classic asp and ajax to create a dependent combo?

+2


source to share


3 answers


Long before the concept of AJAX was formalized, I used a technique called XML Data Islands to achieve this functionality. Obviously there are a number of AJAX frameworks and scripts that can achieve a similar result, but this is just a tried and tested method that I have used over the years in my legacy applications.

I can't quickly find relevant articles, so I dug up some of my old code. Basically, when you set the value of the parent control, you run some javascript that sends the request to a separate ASP page. This page queries the database to determine the data that will be used to populate the child control.

This second page returns the results as XML, which is stored and processed on the XML data island on the home page. The calling javascript then parses the returned XML and populates the child control.

First, we have javascript calls from the main control:

<tr>
                    <td>Customer:</td>
                    <td>
                        <select id="CustomerID" NAME="CustomerID" 
                            onfocus="javascript:populateCombo(fProcess.CustomerID.value)" 
                            onChange="javascript:populateCombo(fProcess.CustomerID.value)"  
                            onkeypress="javascript:populateCombo(fProcess.CustomerID.value);">
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Depot:</td>
                    <td>
                        <select name="depot" id="depot" size="1"  alt="Depot">
                            <option value="" selected >&lt;Select Depot&gt;</option>
                        </select>
                    </td>
                </tr>

      

Then we have javascript that makes a call to the second page (which fetches data from the DB):



// Fill combo with XML data
function populateCombo(par) {
    var currNode;

    XMLID.async = false;

    // Change Data Island source
    strQuery = "Select LocationID, LocationName from Locations where CustomerID='" + par + "' and Active = 1 Order By LocationName";
    XMLID.SRC="/fxdb/common/xmlQuery.asp?strQuery=" + strQuery;

    // Get all "names" from XML data
    objNodeList = XMLID.getElementsByTagName("LocationName");
    objNodeListID= XMLID.getElementsByTagName("LocationID");


    // Fill combo with names
    for (var i=0; i < objNodeList.length; i++) {
        fProcess.depot.options[i]=new Option(objNodeList.item(i).text,objNodeListID.item(i).text);
    }

    // Delete extra entries
    while ( objNodeList.length < fProcess.depot.options.length) {
        fProcess.depot.options[(fProcess.depot.options.length - 1)] = null;
    } 

} 

      

And finally, the page that queries the database itself.

<%@ Language="VBScript" %>
<%
'  Declare all variables.
Option Explicit
Dim strSql,objRS,objField
Dim sConn, oConn
Dim strName, strValue


' Buffer and output as XML.
Response.Buffer = True
Response.ContentType = "text/xml"

' Start our XML document.
Response.Write "<?xml version=""1.0""?>" & vbCrLf

' Set SQL and database connection string.

set oConn=server.createobject("adodb.connection") 
sConn=Application("Connection")
oConn.Open sConn
strSQL = Request.QueryString("strQuery")

set objRS=oConn.execute(strSQL)

' Output start of data.
Response.Write "<database>" & vbCrLf

' Loop through the data records.
While Not objRS.EOF
    ' Output start of record.
    Response.Write "<record>" & vbCrLf
    ' Loop through the fields in each record.
    For Each objField in objRS.Fields
        strName  = objField.Name
        strValue = objField.Value
        If Len(strName)  > 0 Then strName = Server.HTMLEncode(strName)
        If Len(strValue) > 0 Then strValue = Server.HTMLEncode(strValue)
        Response.Write "<" & strName & ">" & vbCrLf
        Response.Write strValue & vbCrLf 
        Response.Write "</" & strName & ">" & vbCrLf
    Next
    ' Move to next city in database.
    Response.Write "</record>" & vbCrLf
    objRS.MoveNext
Wend

' Output end of data.
Response.Write "</database>" & vbCrLf
%> 

      

Edit: Oops! I forgot all the important islands of XML data - add this after the Body tag.

<!-- Data Island-->
<XML ID="XMLID"></XML>

      

+1


source


You might be interested in ajaxed solution .



+2


source


I am assuming the word you were missing, "Asp Combobox ajax" is "Cascade". A concept in which the set of items available in one combination depends on the value selected in the previous combox or another field.

There seems to be no good simple examples in ASP. However, combined with jquery, you can try this link, jquery.cascade cascading values ​​from forms .

This leaves you only having to create ASP pages that generate simple JSON.

0


source







All Articles