|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Dropw down list
Help
Scenario is this: User selects Product #1 to update, this takes him to a form which populates fields for that particular product. i.e product name, price etc (from products table) This page should also have a dropdown list for the categories (popluated from the categories table) but I want the category for the chosen product to be selected by default. (i hope its clear) eg if user selects jaguar XK8 to update, the relevant category that should be selected should be sports car (but the dropdown list should also display the rest of the categories (luxury, economy etc) below is my sql query strSQL1 = "SELECT tblProducts.PID, tblCategory.Category, tblCategory.CategoryID " &_ "FROM tblCategory INNER JOIN tblProducts ON tblCategory.Category = tblProducts.Category" and then my scrip to populate the dropw down list: <% Response.Write "<SELECT Name=""Category"">" Response.Write "<OPTION Value="" ""></OPTION>" ' Loop through the recordset and populate the dropdown while not objRS1.eof if objRS1.fields("CategoryID") = selected then response.write("<OPTION VALUE=" & objRS1.fields("CategoryID") & " selected>") response.write(objRS1.fields("Category") & "</OPTION>") else response.write("<OPTION VALUE=" & objRS1.fields("CategoryID") & ">") response.write(objRS1.fields("Category") & "</OPTION>") end if objRS1.MoveNext wend 'Close the Selection box Response.write "</select>" %> |
|
#2
|
|||
|
|||
|
Compare objRS1.fields("Category") to the form selction. If they are the same, add selected to the option. You can do it with a simple if statement in your code.
|
|
#3
|
|||
|
|||
|
what you want to do is a query statement to lookup all the information on the product - store that info in local variables, so lets say your Category ID is 5 for "Sports Car"
so in your while not loop you simply place Code:
'code to lookup product info here
categoryId = rs("categoryId") 'category to which the product is already assigned
'code to get recordset of all categories here
while not objRS1.eof
if objRS1.fields("CategoryID") = categoryId then
response.write( "<OPTION VALUE=" & objRS1.fields("CategoryID") & " selected>" & objRS1.fields("Category") & "</OPTION>")
else
response.write( "<OPTION VALUE=" & objRS1.fields("CategoryID") & ">" & objRS1.fields("Category") & "</OPTION>")
end if
objRS1.MoveNext
wend
'rest of your code here
%>
|
|
#4
|
|||
|
|||
|
another shorter way would be:
Code:
while not objRS1.eof
response.write( "<OPTION VALUE=" & objRS1.fields("CategoryID") & " <%if objRS1.fields("CategoryID") = categoryId then%>selected<%end if%>>" & objRS1.fields("Category") & "</OPTION>")
objRS1.MoveNext
wend
__________________
Hope this helps. Mike Royal Selangor Pewter "I have not failed. I've just found 10,000 ways that won't work." - Thomas Alva Edison (1847-1931) |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > Dropw down list |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|