highlighting user selected option after form submit

Discussion in 'ASP General' started by brendan.wong@gmail.com, Oct 12, 2006.

  1. Guest

    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
    , Oct 12, 2006
    #1
    1. Advertising

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

    ?




    <> wrote in message
    news:...
    > 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
    >
    Aaron Bertrand [SQL Server MVP], Oct 12, 2006
    #2
    1. Advertising

  3. Mike Brind Guest

    <> wrote in message
    news:...
    > 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
    Mike Brind, Oct 12, 2006
    #3
  4. Evertjan. Guest

    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)
    Evertjan., Oct 12, 2006
    #4
  5. Guest

    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
    %>
    , Oct 12, 2006
    #5
  6. Mike Brind Guest

    <> wrote in message
    news:...
    >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
    Mike Brind, Oct 12, 2006
    #6
  7. 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.




    <> wrote in message
    news:...
    >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
    > %>
    >
    Aaron Bertrand [SQL Server MVP], Oct 12, 2006
    #7
  8. Guest

    gotcha. thanks aaron
    , Oct 12, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B
    Replies:
    9
    Views:
    543
    Neredbojias
    Jul 5, 2009
  2. John
    Replies:
    5
    Views:
    987
    dorayme
    Mar 1, 2010
  3. Mark Kolber
    Replies:
    4
    Views:
    340
  4. Monty
    Replies:
    2
    Views:
    152
  5. Replies:
    3
    Views:
    669
Loading...

Share This Page