Best way to populate a <SELECT>

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Best way to populate a <SELECT>

    Hi All

    I've being doing it my own little way for sometime now, but I'm not sure if
    its the best.

    When I'm doing a recordset for a simple <SELECT> I do it as follows:

    <SELECT NAME="FIRSTNAME" SIZE=1>
    <%
    oRSv.MoveFirst
    Do while not oRSv.EOF
    Response.Write "<OPTION VALUE='"& oRSv("FIRSTNAME") & "'>" &
    oRSv("FIRSTNAME") & "</OPTION>"
    oRSv.MoveNext
    Loop
    %>
    </SELECT>

    When I'm doing a recordset for a <SELECT> which specifies that one is
    selected I do it as follows:

    <SELECT NAME="PIC" SIZE=1>
    <OPTION
    <% IF oRSv("PIC") = "bill" THEN Response.Write "SELECTED " %>
    VALUE="bill">Bill</OPTION>

    <OPTION
    <% IF oRSv("PIC") = "bob" THEN Response.Write "SELECTED " %>
    VALUE="bob">Bob</OPTION>

    <OPTION
    <% IF oRSv("PIC") = "sally" THEN Response.Write "SELECTED " %>
    VALUE="sally">Sally</OPTION>

    etc....

    </SELECT>

    Is there a better way of doing the above?

    Rgds

    Laphan


    Laphan Guest

  2. Similar Questions and Discussions

    1. Dynamically Populate Multiple Select Boxes
      I am trying to develop an application where a user selects the Year, Make and Model of their vehicle. In the past, I had a query that input the...
    2. How can I select items in a datagrid to populate a second datagrid.
      I currently have a datagrid that looks like the following. Sales Table Date Dogs Cats Fish 5/1/2004 4 ...
    3. Populate select field and textbox dynamically - no refresh
      Ive got a db table with the fields Product_Name and Product_Price. I want to display a pulldown containing the product names and display the...
    4. #25474 [Fbk->Opn]: posting arrays from a select box with multiple select is not working properly
      ID: 25474 User updated by: fmuller at cisco dot com Reported By: fmuller at cisco dot com -Status: Feedback...
    5. SELECT DISTINCT + ORDER BY gives ERROR 145: ORDER BY items mustappear in the select list if SELECT DISTINCT is specified.
      Dan, You should be able to do this: SELECT Id, FaxID, ReceivedTime, Pages FROM ( SELECT DISTINCT .Id AS Id,
  3. #2

    Default Re: Best way to populate a <SELECT>

    I would use a routine for this:

    a) Use .GetRows() to return an array from your recordset. Then pass the
    GetRows array into a routine that writes your
    <select> element, and selects the appropriate item An example of such a
    routine is at the end of this post:

    -OR-

    b) Use the .Getstring() function to return a string using appropriate
    delimiters. Here is an example of using getString() to create a HTML table.
    Instead of <td> etc you would use <option> tags:
    [url]http://www.juicystudio.com/tutorial/asp/getstring.asp[/url]

    Cheers
    Ken

    '------------------------------------------------------------
    ' WriteFormSelectList
    '------------------------------------------------------------
    ' Returns HTML for a <select> element
    ' Accepts strName as string
    ' Accepts strID as string
    ' Accepts strHTMLAttributes as string - literal text for any other HTML
    attributes
    ' Accepts form option elements as array
    ' Accepts array format as integer: 0 = rows/cols, 1 = cols/rows (eg from
    getRows)
    ' Accepts optional text for first <option></option> tag
    ' Accepts strSelectedValue as option to be selected
    '------------------------------------------------------------
    Function WriteFormSelectList( _
    ByVal strName, _
    ByVal strID, _
    ByVal strHTMLAttributes, _
    ByVal arrOptions, _
    ByVal intArrayFormat, _
    ByVal strFirstOption, _
    ByVal strSelectedValue _
    )

    Dim i ' array 1st dimension counter

    If not isArray(arrOptions) then
    Exit Function
    End If

    WriteFormSelectList = "<select name=""" & strName & """ ID=""" & strID &
    """"

    If Len(strHTMLAttributes & "") > 0 then
    WriteFormSelectList = WriteFormSelectList & " " & strHTMLAttributes & """"
    End If

    WriteFormSelectList = WriteFormSelectList & ">" & vbCrLf

    If Len(strFirstOption) > 0 then
    WriteFormSelectList = WriteFormSelectList & strFirstOption & vbCrLf
    End If

    If intArrayFormat = 0 then
    For i = 0 to Ubound(arrOptions, 1)
    WriteFormSelectList = WriteFormSelectList & "<option value=""" &
    arrOptions(i,0) & """>" & arrOptions(i,1) & "</option>" & vbCrLf
    Next
    Else
    For i = 0 to Ubound(arrOptions, 2)
    WriteFormSelectList = WriteFormSelectList & "<option value=""" &
    arrOptions(0,i) & """>" & arrOptions(1,i) & "</option>" & vbCrLf
    Next
    End If

    WriteFormSelectList = WriteFormSelectList & "</select>" & vbCrLf

    If Len(strSelectedValue & "") > 0 then
    WriteFormSelectList = Replace(WriteFormSelectList, "value=""" &
    strSelectedValue & """>", "value=""" & strSelectedValue & """ selected>")
    End If

    End Function
    '------------------------------------------------------------
    ' --- WriteFormSelectList
    '------------------------------------------------------------


    "Laphan" <news@DoNotEmailMe.co.uk> wrote in message
    news:OHEmzHYJEHA.3216@tk2msftngp13.phx.gbl...
    : Hi All
    :
    : I've being doing it my own little way for sometime now, but I'm not sure
    if
    : its the best.
    :
    : When I'm doing a recordset for a simple <SELECT> I do it as follows:
    :
    : <SELECT NAME="FIRSTNAME" SIZE=1>
    : <%
    : oRSv.MoveFirst
    : Do while not oRSv.EOF
    : Response.Write "<OPTION VALUE='"& oRSv("FIRSTNAME") & "'>" &
    : oRSv("FIRSTNAME") & "</OPTION>"
    : oRSv.MoveNext
    : Loop
    : %>
    : </SELECT>
    :
    : When I'm doing a recordset for a <SELECT> which specifies that one is
    : selected I do it as follows:
    :
    : <SELECT NAME="PIC" SIZE=1>
    : <OPTION
    : <% IF oRSv("PIC") = "bill" THEN Response.Write "SELECTED " %>
    : VALUE="bill">Bill</OPTION>
    :
    : <OPTION
    : <% IF oRSv("PIC") = "bob" THEN Response.Write "SELECTED " %>
    : VALUE="bob">Bob</OPTION>
    :
    : <OPTION
    : <% IF oRSv("PIC") = "sally" THEN Response.Write "SELECTED " %>
    : VALUE="sally">Sally</OPTION>
    :
    : etc....
    :
    : </SELECT>
    :
    : Is there a better way of doing the above?
    :
    : Rgds
    :
    : Laphan
    :
    :


    Ken Schaefer Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139