I'm using classic asp, I have a drop down list that the user selects and then presses submit. After they press submit the drop down list is going back to the default value instead of what they selected. Is there anyway to keep the state of the drop down between post backs instead of it going back to the default? Can post code sample if needed. Thanks!
A:
You can use javascript and plain HTML to achieve this: HTML: create a hidden field
Javascript: On submit keep the selected value in hidden variable
On page load loop thru the drop down values and set the selected value using the hidden variable
One more variant is available in http://www.daniweb.com/forums/thread105485.html
balalakshmi
2010-03-21 16:30:06
may i know why this was downvoted and without any reason explained?
balalakshmi
2010-03-22 15:32:47
I didn't downvote it, but may be because it is complicating things unnecessarily by requiring javascript, when the OP problem requires only a serverside solution to take into account the posted values of the `select`.
voyager
2010-03-22 16:48:23
@voyager thanks for clarifying. that will help me next time
balalakshmi
2010-03-23 07:07:14
You are welcome, we are all here to help/learn. Glad I could clarify.
voyager
2010-03-23 13:39:18
+3
A:
You have to "select it" serverside according to the values that the user has POSTed.
<select id="cars">
<option value="volvo"
<%
if request.form("cars") = "volvo" then
response.write("selected")
end if %>
>Volvo</option>
<option value="Saab"
<%
if request.form("cars") = "Saab" then
response.write("selected")
end if %>
>Saab</option>
<option value="Mercedes"
<%
if request.form("cars") = "Mercedes" then
response.write("selected")
end if %>
>Mercedes</option>
<option value="Audi" <%
if request.form("cars") = "Audi" then
response.write("selected")
end if %>
>Audi</option>
</select>
Of course, you might want to homegrown your own function to avoid all that boilerplate.
<%
sub option(value, data, select_id)
Response.Write("<option value=""" & value & """)
if request.form(select_id) = value then
Response.Write("selected")
end if
Response.Write(">" & data & "</option>")
end sub
%>
' (...)
<select id="cars">
<% option("volvo", "Volvo", "cars") %>
<% option("Saab", "Saab", "cars") %>
<% option("Mercedes", "Mercedes", "cars") %>
<% option("Audi", "Audi", "cars") %>
</select>
If you pass the function a blank select_id
, it will not care about trying to select the selected item of the select
on postback.
voyager
2010-03-21 16:30:46
I used this function and it was just what I needed worked perfectly! Thank you!!
Bmw
2010-03-22 16:41:26
@Bmw: remember to adapt it to your needs, as there may be some things that I didn't take into account (like for example, if you use GET instead of POST) in order to keep it short. Glad it helped.
voyager
2010-03-22 16:44:41