Somacon.com: Articles on websites & etc.

§ Home > Index > ASP Programming

Print Select Options from ASP

An optimized ASP function for printing select options.

printoptions.asp
<%
'  This code is hereby granted to the public domain.
'
' ** PrintOptions Documentation **
'
' Summary: PrintOptions is an optimized function that prints
'  the OPTIONs list for a SELECT form element.
'
' Parameters:
' objCN [in] - Connection string or opened connection object.
' strsql [in] - A query that retrieves the data for populating
'   the options.  The first column will be the value of each option,
'   and the second column will be the text of each option.
'   Any other columns will be ignored.
' selectedID [in] - A single value whose matching option will
'   be default selected in the options list if it exists.
'
' Notes:
'   * When the values column is an integer, make sure to verify that
'     selectedID is numeric and cast it using CLng().
'   * The values and text of the options will always be HTML encoded.
'   * Nulls will be printed as empty strings.
'
' Usage:
'    <select id="ObjectID">
'       <%  PrintOptions "DSN=MyDSN", _
'              "SELECT ObjectID, ObjectName FROM tblObjects", _
'              CLng(varObjectID)    %>
'    </select>
'
'
' ** PrintOptionsMultiple Documentation **
'
' Summary: PrintOptionsMultiple does the same thing as PrintOptions,
'   except that SelectedID may be a comma-separated list of the values
'   of the options that should be default selected in a SELECT form
'   element having the MULTIPLE keyword.
'   Each value is trimmed and compared as a string.

Function PrintOptions(ByRef objCN, strsql, selectedID)
    PrintOptionsInternal objCN, strsql, selectedID, False
End Function

Function PrintOptionsMultiple(ByRef objCN, strsql, selectedID)
    PrintOptionsInternal objCN, strsql, selectedID, True
End Function

' Do not use this internal function, use the above function stubs.
Function printOptionsInternal(ByRef objCN, strsql, selectedID, bAllowMultiple)
    Dim objRS, ArrayRecords, i
    Dim objDictionary, arraySelected, bIsSelected
    Set objRS = Server.CreateObject("ADODB.Recordset")

    ' Get all values from selectedID as strings into a dictionary
    If bAllowMultiple Then
        Set objDictionary = Server.CreateObject("Scripting.Dictionary")
        arraySelected = Split(Trim(selectedID),",")
        For Each selectedID In ArraySelected
            If Trim(selectedID) <> "" Then
                objDictionary.Add CStr(Trim(selectedID)), CStr(Trim(selectedID))
            End If
        Next
    End If

    objRS.Open strsql,objcn,adOpenForwardOnly,adLockReadOnly,adCmdText

    Do While Not objRS.EOF
        ArrayRecords = objRS.GetRows(50)

        For i = 0 To UBound(ArrayRecords,2)
            bIsSelected = False
            If bAllowMultiple And Not IsNull(ArrayRecords(0, i)) Then
                If objDictionary.Exists(CStr(Trim(ArrayRecords(0, i)))) Then 
                    bIsSelected = True
                End If
            Else
                If ArrayRecords(0, i) = selectedID Then
                    bIsSelected = True
                End If
            End If
            If bIsSelected Then
                Response.write "<option selected"
            Else
                Response.write "<option"
            End If
            Response.write " value="
            Response.write Chr(34)
            If Not IsNull(ArrayRecords(0, i)) Then
                Response.write Server.HTMLEncode(ArrayRecords(0, i))
            End If
            Response.write Chr(34)
            Response.write ">"
            If Not IsNull(ArrayRecords(1, i)) Then
                Response.write Server.HTMLEncode(ArrayRecords(1, i))
            End If
            Response.write "</option>"
        Next
        Response.write vbCrLf
    Loop
    objRS.Close
    Set objRS = Nothing
    If bAllowMultiple Then
        objDictionary.RemoveAll
        Set objDictionary = Nothing
    End If
End Function

%>

ASP Speed Tricks Site Map

  1. ASP Speed Tricks
  2. Optimized Table Printing in ASP
  3. Optimized Heirarchical Table Printing in ASP
  4. Print Javascript Array from ASP
  5. Print Select Options from ASP You Are Here
  6. Javascript Autocomplete Combobox - find as you type
  7. ASP Speed Tricks Appendix
  8. ASP Speed Tricks PDF Format

Have you heard of the new, free Automated Feeds offered by Google Merchant Center? Learn more in Aten Software's latest blog post comparing them to traditional data feed files.
Created 2005-05-08, Last Modified 2011-07-24, © Shailesh N. Humbad
Disclaimer: This content is provided as-is. The information may be incorrect.