Home All Groups Group Topic Archive Search About

highlighting user selected option after form submit

Author
12 Oct 2006 5:29 PM
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

Author
12 Oct 2006 5:36 PM
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))

?




<brendan.w***@gmail.com> wrote in message
Show quoteHide quote
news:1160674161.807700.182900@i42g2000cwa.googlegroups.com...
> 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
>
Are all your drivers up to date? click for free checkup

Author
12 Oct 2006 5:38 PM
Mike Brind
<brendan.w***@gmail.com> wrote in message
Show quoteHide quote
news:1160674161.807700.182900@i42g2000cwa.googlegroups.com...
> 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>


--
Mike Brind
Author
12 Oct 2006 5:42 PM
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>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Author
12 Oct 2006 6:05 PM
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
%>
Author
12 Oct 2006 6:17 PM
Mike Brind
<brendan.w***@gmail.com> wrote in message
Show quoteHide quote
news:1160676339.764608.260720@h48g2000cwc.googlegroups.com...
>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
> %>
>

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).


--
Mike Brind
Author
12 Oct 2006 6:23 PM
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.




<brendan.w***@gmail.com> wrote in message
Show quoteHide quote
news:1160676339.764608.260720@h48g2000cwc.googlegroups.com...
>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
> %>
>
Author
12 Oct 2006 6:54 PM
brendan.wong
gotcha.  thanks aaron

Bookmark and Share