Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop down List retaining value after Post 1

Status
Not open for further replies.

mana2

Programmer
Aug 9, 2002
116
US
Hi,

I have a drop down list and once it's submitted, I'd like to display it again with the selected value. This works but then I have the value listed twice.

<select name="position">
<option selected>Choose One</option>
<option value="director">director</option>
<option value="superintendent">superintendent</option>
<option value="administrator">administrator</option>
<option value="other">other</option>
</select>

After Posting Form:


<select name="position">
<option value="<%=Server.HTMLEncode(Request.Form("position"))%>" ><%=Server.HTMLEncode(Request.Form("position"))%></option>
<option value="director">director</option>
<option value="superintendent">superintendent</option>
<option value="administrator">administrator</option>
<option value="other">other</option>
</select>

Thanks
 
You must evaluate each option with the value of request.form("position"), and if they are the same insert "selected" in the option.



BTW: Sometimes this is desired behavior, especially when the dropdown list is long.
 
further to what fox said, here's the code:

Code:
<%
selPOS = Server.HTMLEncode(Request.Form("position"))
%>
<select name="position">
<option value="director" <% if selPOS = "director" then response.write "selected=""selected"""%>>director</option>
' ... and so on for each option
</select>


--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
Thank you so much to both of you. I'll try it out.
 
You'll encounter this issue again and again. So i include a "user defined functions" to my scripts. One of the functions is:
Code:
function Selected( cOne, cTwo)
if rtrim(cOne) = rtrim(cTwo) then
 Selected = " selected "
end if
end function

Build a pulldown:

Code:
selPOS = Server.HTMLEncode(Request.Form("position"))

cSQL = "SELECT * FROM [position]"
set rs = server.CreateObject("ADODB.Recordset")
rs.open cSQL, conn
do while not rs.eof
 response.Write "<option " & _
   selected( rs("position"), selPOS) )  & _
   " value='" & rs("position") & "'>" & _
   rs("position") 
 rs.movenext
loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top