Small API Tag helper for Select and OPTION tags

 

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
March 26, 2000
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Small API tag helper for SELECT and OPTION tags by Salim N.

Attached is a small API that is a great helper for ASP programmers. It's a SELECT tag helper and contains advanced features such as Multiple OPTION caption for the Same OPTION value. It's not the most advance script routine in the world but it sure is helpful and a clean way to program.

Here is the code

<%
'------------------------------------------------------------------------------
'This is a generic <SELECT> Element Helper function
'------------------------------------------------------------------------------
'Written By: Salim Naim 20/3/2000
'Note: I'm more than happy to fix any Bugs or answer any questions regarding the
'sample,i can be reached at snaim@zfp.com or salimn@yahoo.com

'------------------------------------------------------------------------------
'Example Use 1:   
'Simple basic usage
'Call OutputSelect("country","COUNTRYNAME","COUNTRYNUM","","SELECT * FROM COUNTRIES","DSN=..")
'------------------------------------------------------------------------------
'Example Use 2:   
'This will output multiple option captions for the <OPTION> selection
'Call OutputSelect("USERS","FIRSTNAME,LASTNAMSE","USERNUM","","SELECT * FROM USERS","DSN=..")
'Note: the field captions, must be delimited by a coma ,
'------------------------------------------------------------------------------
'######    USE THE szSelected PARAMETER TO SET A DEFAULT SELECTED OPTION #######
'------------------------------------------------------------------------------

'---------------------------------------------------------------------------------------
' Function:        OutputSelect
' Description:    This Functin is a Helper function that outputs
'                 a <select>..</select> based on your query.
'---------------------------------------------------------------------------------------
' Parameters:    name             -     This is the HTML ATTRIBUTE for NAME                
'                 fiedlcaption     -    These are the Fieldsnames from the Database that
'                                     you want to output in the <option>.
'                                     {Note: can contain multiple, use [,] as a delimeter.)   
'                 fieldvalue         -    This is the Database fieldname that you want to be as
'                                     the VALUE ATTRIBUTE.   
'                 szSelected         -    The SELECTED VALUE from the List.
'                 szQry             -    The SQL Statment to populate the LIST from
'                 szDSN             -    The Data Source Name for the RecordSet   
'---------------------------------------------------------------------------------------
Sub OutputSelect(name,fieldcaption,fieldvalue,szSelected,szQry,szDSN)
Dim objRS
Dim aCaptions,szCaption
Dim i

'first split all the captions into an array.
aCaptions = split(fieldcaption,",")

'create our recordset.
Set objRS = Server.CreateObject("ADODB.RecordSet")
if Not IsObject(objRS) then
Response.Write "Out of Resources"
Response.End
End if

'execute the Query
objRS.Open szQry,szDSN
if Not objRS.EOF then%>
<%'start the list, output the NAME%>   
<select name="<%=name%>">
    <option value=""></option>
<%While Not objRS.EOF%>        
    <%'only output the values that are not selected
    if Not szSelected = objRS(fieldvalue) then%>
        <%'in the case that the value is empty output the caption as the value
        if Len(Trim(fieldvalue)) > 0 then%>
            <option value="<%=objRS(fieldvalue)%>">
            <%'Output every caption that is in the array
            for each szCaption in aCaptions%>
                <%if Len(Trim(szCaption)) > 0 then%>
                <%=objRS(szCaption)%>&nbsp;
                <%end if%>
            <%next%>
            </option>
        <%else%>
            <option value="<%=objRS(fieldcaption)%>">
            <%for each szCaption in aCaptions%>                
                <%if Len(Trim(szCaption)) > 0 then%>
                <%=objRS(szCaption)%>&nbsp;
                <%end if%>                
            <%next%>    
            </option>
        <%end if%>
    <%'the selecte values
    else%>
        <option value="<%=objRS(fieldvalue)%>" selected>
        <%for each szCaption in aCaptions%>            
            <%if Len(Trim(szCaption)) > 0 then%>
            <%=objRS(szCaption)%>&nbsp;
            <%end if%>            
        <%next%>   
        </option>
    <%end if%>       
    <%objRS.MoveNext   
Wend%>
</select>
<%End if
'close up everything
Call objRS.Close()
'free resources.
Set objRS = Nothing
End Sub%>
blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials