Hi,
I created a page the has drop menus based on database recordsets that filter each other . The only problem i run into is keeping the only problem i'm having is holding the view state of the first dropmenu after it reloads. I've tried cookies, session states...am i doing something wrong?? this is my code for the cookies and the form:

<% select=request.cookies("select")
if select="false"
then end if
%>
<form name="form1" method="post" action="" >
<p> Make
<select name="select" id="select" value="select">
<%
While (NOT Recordset1.EOF)
%>
<option value="<%=(Recordset1.Fields.Item("VechicleMakeID").Value)%>"><%=(Recordset1.Fields.Item("VechicleMake").Value)%></option>
<%
Recordset1.MoveNext()
Wend
If (Recordset1.CursorType > 0) Then
Recordset1.MoveFirst
Else
Recordset1.Requery
End If
%>
</select>
</p>
<p>Model
<select name="select2" id="select2">
<%
While (NOT Recordset2.EOF)
%>
<option value="<%=(Recordset2.Fields.Item("ModelID").Value)%>"><%=(Recordset2.Fields.Item("Model").Value)%></option>
<%
Recordset2.MoveNext()
Wend
If (Recordset2.CursorType > 0) Then
Recordset2.MoveFirst
Else
Recordset2.Requery
End If
%>
</select>
</p>
<p>parts
<select name="select3">
</select>
</p>
<p>
<input type="submit" name="Submit" value="Continue" >
</p>
</form>
<%
Response.cookies("select")=select
%>


Any Ideas??

Dani AI

Generated

Good summary of the problem from : the browser forgets which option was chosen because the server rebuilds the <select> without marking the previously chosen option as selected. The simplest, most reliable fix is server-side: capture the posted value and, when you render each <option>, add the selected="selected" attribute to the matching option. That preserves the choice on the same postback without needing cookies or sessions.

Key points and a minimal pattern to follow:

  • Read the posted value at the top of the page (use Request.Form if your form is POST).
  • Use meaningful names (e.g., make, model) instead of generic select. Also remove the invalid value attribute from the <select> tag — only <option> elements have value.
  • When looping the recordset, compare each option value to the captured value and emit selected="selected" for the match.

Example of the two critical bits (keep them near the top and inside your option output):

<% selectedMake = Request.Form("make") %>

<option value="<%= rs("VechicleMakeID") %>"<% If rs("VechicleMakeID") = selectedMake Then Response.Write " selected=\"selected\"" End If %>><%= rs("VechicleMake") %></option>

If you want the page to reload as soon as a make is chosen (as suggested), add onchange="this.form.submit()" on the make <select> and let the server repopulate the dependent lists using the posted value. If you choose cookies for cross-page persistence, set the cookie expiration (for example Response.Cookies("make").Expires = Date + 30) and remember a cookie you set is only available via Request.Cookies on the next request — for the current render keep the posted value in a variable.

Troubleshooting: dump Request.Form("make") while developing to confirm you’re getting the expected value; inspect the final HTML with the browser dev tools to verify the selected attribute is present; ensure your recordset values exactly match the posted string (trim or cast types if needed).

Recommended Answers

All 2 Replies

You need to capture the value of the first select, then test that for each value through the loop when generating the select options. If the value match then set the option selected attibute to selected. Example:

select1 = request.form("select")
While (NOT Recordset1.EOF)
	response.write "<option value=""" & Recordset1("VechicleMakeID")
	if select1 = Recordset1("VechicleMakeID") then
		response.write "selected = """selected""" 
	end if
	response.write """>" & Recordset1("VechicleMake") & "</option>"
Recordset1.MoveNext()
Wend

set the select box to submit the form. then have the next select box request.form("firstselectvalue") it. that way you can set up a if select1 hasnt been submited the disable select 2 etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.