LouManizer
LouManizer

Reputation: 51

The dropdown value doesn't show selected value

I have to populate the values to drop down box from SQL. Classic ASP is the language I need to use. I was able to populate it; however, I am not being able to keep the selected value in the drop down. I kept the value "selected" after "option value" inside the loop but the issue after doing this was: all the prior options keep on loading (selecting) in drop down until the one I selected comes in the list. Can anyone suggest/comment where/what am I missing?

    Categories:<select name="menu" onChange="if(options[selectedIndex].value != 0){location='test.asp?list=bybrand&brandID=' + options[selectedIndex].value + '', exit=false;}">
    <OPTION value=0 selected>-- SELECT --</OPTION>
    <% dim rs
        SQL = "SELECT DISTINCT(brand) as brand FROM tblprdt"
        SET rs=objConn.Execute(SQL)  

        IF NOT (rs.BOF and rs.EOF) THEN 
            WHILE NOT rs.EOF
                Response.Write "<option value=""" & replace(rs("brand")," & ","@") & """ selected >" & rs("brand") & "</option>"
                rs.MoveNext
            WEND 
        ELSE
            Response.Write "<option >No categories available</option>"
        END IF
        rs.close
        SET rs = nothing
        %>
    </select>

Upvotes: 0

Views: 305

Answers (1)

John
John

Reputation: 4638

Assuming that the value BrandId corresponds to a key field in your table tblprdt and that field is also called BrandId, change your query to

SQL = "SELECT BrandId, brand FROM tblprdt"

then later

      WHILE NOT rs.EOF
        if cint(rs("BrandId")) = cint(Request.Querystring("BrandId")) then
            Response.Write "<option value=""" & replace(rs("BrandId")," & ","@") & """ selected >" & rs("brand") & "</option>"
        else
            Response.Write "<option value=""" & replace(rs("BrandId")," & ","@") & ">" & rs("brand") & "</option>"
        end if
            rs.MoveNext
        WEND 

EDIT I've just tested the js in your example and it just passes the value of rs("brand"), so it's much simpler. Leave your query as it is and use

  WHILE NOT rs.EOF
    if replace(rs("BrandId")," & ","@") = Request.Querystring("BrandId") then
        Response.Write "<option value=""" & replace(rs("Brand")," & ","@") & """ selected >" & rs("brand") & "</option>"
    else
        Response.Write "<option value=""" & replace(rs("brand")," & ","@") & ">" & rs("brand") & "</option>"
    end if
        rs.MoveNext
    WEND 

Upvotes: 1

Related Questions