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)%> <%end if%> <%next%> </option> <%else%> <option value="<%=objRS(fieldcaption)%>"> <%for each szCaption in aCaptions%> <%if Len(Trim(szCaption)) > 0 then%> <%=objRS(szCaption)%> <%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)%> <%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%> |
|