I'm using classic asp, I ha开发者_运维问答ve 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!
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.
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
精彩评论