Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > highlighting user selected option after form submit

Reply
Thread Tools

highlighting user selected option after form submit

 
 
brendan.wong@gmail.com
Guest
Posts: n/a
 
      10-12-2006
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

 
Reply With Quote
 
 
 
 
Aaron Bertrand [SQL Server MVP]
Guest
Posts: n/a
 
      10-12-2006
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))

?




<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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
>



 
Reply With Quote
 
 
 
 
Mike Brind
Guest
Posts: n/a
 
      10-12-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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


 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      10-12-2006
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%>><%=optionArr ay(n)%></option>
<%
next
%>
</select>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
brendan.wong@gmail.com
Guest
Posts: n/a
 
      10-12-2006
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
%>

 
Reply With Quote
 
Mike Brind
Guest
Posts: n/a
 
      10-12-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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


 
Reply With Quote
 
Aaron Bertrand [SQL Server MVP]
Guest
Posts: n/a
 
      10-12-2006
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.




<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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
> %>
>



 
Reply With Quote
 
brendan.wong@gmail.com
Guest
Posts: n/a
 
      10-12-2006
gotcha. thanks aaron

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
selected=selected in the center of option list John HTML 5 03-01-2010 08:30 PM
submit 1 form to 2 servers or 2 forms to 2 server (1 form each) on 1 submit abansal.itp@gmail.com Javascript 3 06-23-2007 07:29 AM
Changing 'selected' Option in Select Dropdown before Submit Monty Javascript 2 11-20-2005 07:43 PM
Dynamically update SELECTED option in option list Mark Kolber Javascript 4 01-01-2005 02:07 PM
Highlighting Selected row on Edit Mode =?Utf-8?B?UGF0cmljay5PLklnZQ==?= ASP .Net 2 11-15-2004 01:27 AM



Advertisments