?
Solved

ASP - How to assign "selected" values to a combo box

Posted on 2003-03-17
6
Medium Priority
?
192 Views
Last Modified: 2010-04-06
I'm pulling data from a recordset, but I would like the combo box values on my ASP page to automatically "select" the proper value, as listed in the recordset.  This page is used for users to go back and "edit" their account.

For example: The recordset value for the field is "Blue" ... Therefore, in the combo box, the value that's selected will be "Blue".  The order of the combo box values must maintain consistency.

If there's a simple way of doing this without using multiple "If Then" statements, that would be cool.  Thank you!
0
Comment
Question by:tommy10101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Accepted Solution

by:
arthuryeung earned 160 total points
ID: 8156467
I think it is not possible to do it without if..then statement, but you can write a function like this to help you:

'============================================================================================================
' Function          : MakeOption(String,String,Boolean)
' Description     : Make an HTML FORM SELECT OPTION with specified label, value and selected state
' Input(s)          : label                    = the LABEL attribute of the OPTION
'                      value                    = the VALUE attribute of the OPTION
'                      selected_value     = if the value match with the select value the this option is SELECTED
' Return          : Boolean               = whether the input date is valid for SQL datetime
'============================================================================================================
sub MakeOption(label, value, selected_value)
     response.write "<option value=""" & value & """"
     if value = selected_value then response.Write " selected"
     response.write ">" & label & "</option>" & vbcrlf
end sub

In your ASP:

<SELECT NAME="color">
<% MakeOption "Blue","Blue",rs("color") %>
<% MakeOption "Yellow","Yellow",rs("color") %>
<% MakeOption "Green","Green",rs("color") %>
</SELECT></TD>
0
 
LVL 1

Expert Comment

by:cwvsmart
ID: 8156585
<%
Dim con, rs

'Database connection info and driver (if this driver does not work then comment it out and use 'one of the alternative drivers)
'con = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath(yourdatabase.mdb)


'This one is if you convert the database to Access 97    
'strCon = "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & Server.MapPath(yourdatabase.mdb)
     
'If you wish to use DSN then comment out the driver above and uncomment the line below (DSN is 'slower than the above drivers)
'con = "DSN=yourDSNname" 'Place the DSN name after the DSN=


'Creating a connection with the Access 2000 Database
'I'm using ACCESS 2000
Set con = Server.CreateObject("ADODB.Connection")
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("yourdatabase.mdb")


SQL = "SELECT * FROM yourTableName"
Set rs = con.Execute(SQL)
While Not rs.EOF
%>
<Select Name="DropDown">
<option value="<%=rs("fldCategory")%>"><%=rs("fldCategory")%>
     <%
     rs.MoveNext
     Wend
     rs.Close
     %>
</Select>
Try whether this suits you if i understand correctly....
0
 
LVL 4

Expert Comment

by:arthuryeung
ID: 8156609
cwvsmart:
i am not sure whether you got the meaning of the question but your code does go wrong:

While Not rs.EOF
%>
<Select Name="DropDown">
<option value="<%=rs("fldCategory")%>"><%=rs("fldCategory")%>
    <%
    rs.MoveNext
    Wend
    rs.Close
    %>
</Select>

you shouldn't put <select xxx> inside the while loop, and it is better to add the close tag </option>, it should be like that:

<Select Name="DropDown">
<%
While Not rs.EOF
%>
<option value="<%=rs("fldCategory")%>"><%=rs("fldCategory")%></option>
    <%
    rs.MoveNext
    Wend
    rs.Close
    %>
</Select>

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:tommy10101
ID: 8156620
Thanks to both of you!  It's working now. Arthur, I accepted your answer as it was the first one that worked for me.  Very simple solution too...

0
 
LVL 4

Expert Comment

by:CRagsdell
ID: 8156623
Create a recordset of all colors (rsColors), and another recordset of the user info (rsUsers) that includes the color field, then use similar code to the following to determine if the user color equals the particular color being built into the select option list, and then set the selected tag if they are equal.

Response.Write "<select size=""1"" name=""Color"">"

Response.Write "<option value=""0"">-Select A Color-</option>"

rsColors.MoveFirst

Do While Not rsColors.EOF

Response.Write "<option "

If rsUsers.Fields("Color") = rsColors.Fields("Color") Then
Response.Write "selected "
End If

Response.Write "value="
Response.Write "'" & rsColors.Fields("Color") & "'"">" & rsColors.Fields("Color") & "</option><br>"

rsColors.MoveNext

Loop

Response.Write "</select>"

You will note that I have a "Select A Color" as the first option. You cal leave it out if you want.

Of course, be sure to close the recordsets, etc...

CR
0
 
LVL 4

Expert Comment

by:CRagsdell
ID: 8156630
Ooops, too slow on the Submit!

Sorry...

CR
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question