Dropdown list in ASP

M

mstery

This is probably really stupid, but I cannot figure it out, and none of
the references I've found seem quite right for what I'm trying to do.
How do I do the below, but have the option values and Option Choice
Text Description pull from the db instead of requiring manual data
entry?

<option value="1" <%If (Not isNull((RS.Fields.Item("ID").Value))) Then
If ("1" =CStr((RS.Fields.Item("ID").Value))) Then
Response.Write("SELECTED") : Response.Write("")%>>Option Choice Text
Description</option>

Can anybody point me to some resources for this? Thanks in advance.
 
B

Bob Barrows [MVP]

This is probably really stupid, but I cannot figure it out, and none
of the references I've found seem quite right for what I'm trying to
do. How do I do the below, but have the option values and Option
Choice Text Description pull from the db instead of requiring manual
data entry?

<option value="1" <%If (Not isNull((RS.Fields.Item("ID").Value))) Then
If ("1" =CStr((RS.Fields.Item("ID").Value))) Then
Response.Write("SELECTED") :

Why could ID be Null? Why don't you exclude Null ID's in the query's
WHERE clause?

It looks like you really want to compare ID to Value, correct? If ID =
Value, then make the option selected, right?
Response.Write("")%>>Option Choice Text
Description</option>

You neglected to show us the sql statement that produced that recordset.
As a result I'm not going to be able to give you the most efficient
solution, which involves using GetRows. Instead, I'm going to resort to
an inefficient recordset loop and, as you did not provide the name of
the field that contains the option text, I'm going to invent a field
name - OptionText. Substitute the actual name of the field


Try thinking of the problem this way: you're just creating a string of
text that happens to contain html. It's not a mystery.

My preference is to build the options totally in server-side code,
storing the string in a variable and then response.writing the variable
into the select tag. Something like;

<%
....
dim options
options = "<option"
do until rs.eof
options=options & " value = """ & rs("Value") & """"
if rs("ID") = rs("Value") then
options=options & " SELECTED"
end if
options=options & ">" & rs("OptionText") & "<option"
rs.movenext
loop
rs.close: set rs=nothing
'close and destroy the connection here also
options=left(options, len(options) - 7)
%>
<html><body>
<select>
<%=options%>
</select>
</body></html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top