Simplify loading stored <OPTIONS> values via ASP

 

When loading an <OPTIONS> list from a database table, it's much easier to 
call a procedure that uses process parameters. This way, you can use the same 
procedure for several different operations. For example, suppose you want to 
fill a Listbox with a list of animals. In addition, you've set up several checkboxes on 
a Web page that let the user simply view the list, or load the list with one of the animals 
already selected.  The following example shows how you might process the resulting 
page. (To save space, we hard-coded the two parameters.)
 
<!-- #include File="DBConnect.Asp" -->
<HTML>
<BODY>
<FORM>
<SELECT name=Atype size= 5>
<%
Dim chkMode
chkMode = 2  'or Request.Form("chkMode")
fillOptionList chkMode,"Snake"
%>
</SELECT>
<%
Sub fillOptionList(myMode, optionValue)
Dim rsAnimal
Dim strSelected
set rsAnimal = CreateObject("ADODB.recordset")
With rsAnimal
    .Open "tblAnimals", strDB, adOpenStatic,,adCmdTable
    Do While Not .EOF
       Select Case myMode
          Case 1
            strSelected = ""
          Case Else
            If optionValue = rsAnimal("Animal") Then
              strSelected = " SELECTED"
            Else
              strSelected = ""
            End if
       End Select
        Response.Write("<OPTION" & strSelected & " VALUE=" _
          & rsAnimal("ID") & ">" & rsAnimal("Animal") & "</OPTION>")
        .MoveNext
         Loop
      .Close
End With
Set rsAnimal = Nothing
End Sub
%>
</BODY>
</HTML>
 
For the OPTION elements, this code produces output HTML like this:

<SELECT name=Atype size= 5>
<OPTION Value=1>Cat</OPTION>
<OPTION Value=2>Cow</OPTION>
<OPTION Value=3>Dog</OPTION>
<OPTION SELECTED Value=4>Snake</OPTION>
<OPTION Value=5>Elephant</OPTION>
<OPTION Value=6>Fish</OPTION>
</SELECT>
  


Active Server Index

Main Index

Search RD Techbase