highlighting user selected option after form submit

B

brendan.wong

hello. i have a really simple form that asks the user to select a
Month from a dropdown. the first time a user visits the page, the
highlighted option should be October, which works fine. then, if the
user submits the form, i want the dropdown to highlight whatever
selection that the user made in the resulting page. for example, if
the user chose February, then the code in the resulting page would look
something like <option value="February" selected>.

here's my code:

<%
dim queryMonth
if (Request.querystring("eventMonth") <> "" ) then
queryMonth = Request.querystring("eventMonth")
else
queryMonth = Month(Now())
end if
%>

<form action="events_new.asp" method="get" onSubmit="return
checkRequiredFields(this);">
<select name="eventMonth">
<option value="">-- Month --</option>
<%
dim i
for i = 1 to 12
if (i = queryMonth) then
response.Write("<option value='" & i & "' selected>" & MonthName(i)
& "</option>")
else
response.Write("<option value='" & i & "'>" & MonthName(i) &
"</option>")
end if
next
%>
</select>
<input type="submit" value="Go">
</form>

However, after performing some tests, i'm not getting the effect that i
want. It only works the first time I visit the page. otherwise, if i
submit the form, the "selected" entry in the dropdown is always the
first option <option value="">--Month--</option>. Anyone know what's
going on? Thanks
 
A

Aaron Bertrand [SQL Server MVP]

Did you try basic debugging? Like,

response.write queryMonth

? Did you try making sure you were comparing apples to apples, like

if (clng(i) = clng(queryMonth))

?
 
M

Mike Brind

hello. i have a really simple form that asks the user to select a
Month from a dropdown. the first time a user visits the page, the
highlighted option should be October, which works fine. then, if the
user submits the form, i want the dropdown to highlight whatever
selection that the user made in the resulting page. for example, if
the user chose February, then the code in the resulting page would look
something like <option value="February" selected>.

here's my code:

<%
dim queryMonth
if (Request.querystring("eventMonth") <> "" ) then
queryMonth = Request.querystring("eventMonth")
else
queryMonth = Month(Now())
end if
%>

<form action="events_new.asp" method="get" onSubmit="return
checkRequiredFields(this);">
<select name="eventMonth">
<option value="">-- Month --</option>
<%
dim i
for i = 1 to 12
if (i = queryMonth) then
response.Write("<option value='" & i & "' selected>" & MonthName(i)
& "</option>")
else
response.Write("<option value='" & i & "'>" & MonthName(i) &
"</option>")
end if
next
%>
</select>
<input type="submit" value="Go">
</form>

However, after performing some tests, i'm not getting the effect that i
want. It only works the first time I visit the page. otherwise, if i
submit the form, the "selected" entry in the dropdown is always the
first option <option value="">--Month--</option>. Anyone know what's
going on? Thanks

<select name="eventMonth">
<option value="">-- Month --</option>
<%
dim i
for i = 1 to 12
Response.Write "<option value='" & i & "'"
If i = Cint(queryMonth) Then Response.Write " selected"
Response.Write ">" & MonthName(i) & "</option>" & vbcrlf
next
%>
</select>
 
E

Evertjan.

wrote on 12 okt 2006 in microsoft.public.inetserver.asp.general:
hello. i have a really simple form that asks the user to select a
Month from a dropdown. the first time a user visits the page, the
highlighted option should be October, which works fine. then, if the
user submits the form, i want the dropdown to highlight whatever
selection that the user made in the resulting page. for example, if
the user chose February, then the code in the resulting page would look
something like <option value="February" selected>.

Something like this comes from one of my pages:

<select name='theSelection'
style='background-color:#ebddb0;color:#826729;'>
<%
for n=0 to to ubound(optionArray)-1
if request.form("theSelection") = optionArray(n) then
selec = " selected style='color:#c00;background-color:#fbedc0;'"
else
selec = ""
end if
%>
<option value='<%=optionArray(n)%>'<%=selec%>><%=optionArray(n)%></option>
<%
next
%>
</select>
 
B

brendan.wong

i did a CInt on queryMonth, and that worked perfectly. thanks to all.
Did you try making sure you were comparing apples to apples, like
if (clng(i) = clng(queryMonth))

just one lingering question though...in the code below, since i'm
basically performing the same test, why would this work and not my
actual code? thanks

<%
if (1 = "1") then
response.Write("asdf") //result is this line of code
else
response.Write("rarr")
end if
%>
 
M

Mike Brind

i did a CInt on queryMonth, and that worked perfectly. thanks to all.


just one lingering question though...in the code below, since i'm
basically performing the same test, why would this work and not my
actual code? thanks

<%
if (1 = "1") then
response.Write("asdf") //result is this line of code
else
response.Write("rarr")
end if
%>

Because items in the the Request collections are passed as strings. That's
what Aaron meant by comparing apples with apples. You were comparing a
numeric type with a string.

If you ever get stuck on what datatype a variable is, just Response.Write
TypeName(variable).
 
A

Aaron Bertrand [SQL Server MVP]

It's not the same test. You implicitly defined both constants here. In
your original code, you brought in a string from the request collection
(this is NOT implicitly declared) and compared it to something that had been
explicitly cast as a number due to the for loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top