Hyperjase
Hyperjase

Reputation: 127

Classic ASP - Retrieve Session variable in drop down

I have a little issue I'm stuck with and can't figure out the best way of resolving this issue. I have a drop down list, which is populated from an SQL result set. When the user proceeds to the next page, it keeps the "shortcode" in a session to be used over the next few steps, but when the user goes back to the new test page, I need the drop down menu to hold the username of the person testing (via the session created earlier on). How would I achieve this?

Here is my asp code:

Operator<br />
    <select name="operator" class="rounded" >
    <optgroup label="Please select the operator">
    <%
    sql = "SELECT * FROM [ProductTestData].[dbo].[CLS_Test_Users]"
    rs.Open sql,conn
    While not rs.eof
        response.write("<option value=" & rs("shortcode") & ">" & rs("username") & "</option>")
        rs.movenext
    Wend
    rs.close
    %>
    </optgroup>
    </select>

The "shortcode" is just the user's initials which are eventually converted back to their full name to insert into SQL. The "shortcode" is stored in session("operator")

Upvotes: 0

Views: 664

Answers (1)

Salman Arshad
Salman Arshad

Reputation: 272106

You need to add the selected attribute on the option that needs to be selected. Here is how:

While not rs.eof
    response.write("<option value=" & rs("shortcode"))
    if rs("shortcode") & "" = session("operator") then
        response.write(" selected")
    end if
    response.write(">" & rs("username") & "</option>")
    rs.movenext
Wend

Upvotes: 1

Related Questions