Drop Down Default Selected

Discussion in 'ASP General' started by JayB, Oct 8, 2004.

  1. JayB

    JayB Guest

    I am creating an online survey for one of our grants and one of the items to
    be answered is county (U.S.). The county is to be in a drop-down menu to
    prevent typos and counties from other states by mistake. I'm doing
    server-side validation (many of our users have disabled JavaScript) and need
    to retain the user's selection if they need to revisit the page to make
    corrections.

    In the past I'd declare variables for each choice in the drop-down menu
    (usually they are no more than five or six choices for what I've done) and
    use a select case to set one of the variables to ' SELECTED'. However, in
    the county list there are 86 choices.

    What would be the most efficient way to retain the users selection if they
    need to go back to fix errors? I also am looking for a similar solution for
    many checkboxes.

    Thanks,

    --
    JayB
    JayB, Oct 8, 2004
    #1
    1. Advertising

  2. What does the code look like that you're using the display the dropdown box?
    Do these values from a recordset, an array, just hard-coded ino the HTML,
    something else?

    Ray at work

    "JayB" <> wrote in message
    news:7Kx9d.8785$...
    >I am creating an online survey for one of our grants and one of the items
    >to be answered is county (U.S.). The county is to be in a drop-down menu to
    >prevent typos and counties from other states by mistake. I'm doing
    >server-side validation (many of our users have disabled JavaScript) and
    >need to retain the user's selection if they need to revisit the page to
    >make corrections.
    >
    > In the past I'd declare variables for each choice in the drop-down menu
    > (usually they are no more than five or six choices for what I've done) and
    > use a select case to set one of the variables to ' SELECTED'. However, in
    > the county list there are 86 choices.
    >
    > What would be the most efficient way to retain the users selection if they
    > need to go back to fix errors? I also am looking for a similar solution
    > for many checkboxes.
    >
    > Thanks,
    >
    > --
    > JayB
    >
    Ray Costanzo [MVP], Oct 8, 2004
    #2
    1. Advertising

  3. JayB

    JayB Guest

    Hard-coded HTML.

    --
    JayB


    "Ray Costanzo [MVP]" <my first name at lane 34 dot commercial> wrote in
    message news:%...
    > What does the code look like that you're using the display the dropdown
    > box? Do these values from a recordset, an array, just hard-coded ino the
    > HTML, something else?
    >
    > Ray at work
    >
    > "JayB" <> wrote in message
    > news:7Kx9d.8785$...
    >>I am creating an online survey for one of our grants and one of the items
    >>to be answered is county (U.S.). The county is to be in a drop-down menu
    >>to prevent typos and counties from other states by mistake. I'm doing
    >>server-side validation (many of our users have disabled JavaScript) and
    >>need to retain the user's selection if they need to revisit the page to
    >>make corrections.
    >>
    >> In the past I'd declare variables for each choice in the drop-down menu
    >> (usually they are no more than five or six choices for what I've done)
    >> and use a select case to set one of the variables to ' SELECTED'.
    >> However, in the county list there are 86 choices.
    >>
    >> What would be the most efficient way to retain the users selection if
    >> they need to go back to fix errors? I also am looking for a similar
    >> solution for many checkboxes.
    >>
    >> Thanks,
    >>
    >> --
    >> JayB
    >>

    >
    >
    JayB, Oct 8, 2004
    #3
  4. Well that kinda sucks... What you can do is:
    <%
    Dim s
    s = Request.Form("dropdownName")
    %>
    <select name="dropdownName">
    <option value="1"<% If s = "1" Then Response.Write " selected"%>>Option
    1</option>
    <option value="2"<% If s = "2" Then Response.Write " selected"%>>Option
    2</option>
    ...
    </select>

    Ray at work

    "JayB" <> wrote in message
    news:cly9d.8794$...
    > Hard-coded HTML.
    >
    > --
    > JayB
    >
    >
    > "Ray Costanzo [MVP]" <my first name at lane 34 dot commercial> wrote in
    > message news:%...
    >> What does the code look like that you're using the display the dropdown
    >> box? Do these values from a recordset, an array, just hard-coded ino the
    >> HTML, something else?
    >>
    >> Ray at work
    >>
    >> "JayB" <> wrote in message
    >> news:7Kx9d.8785$...
    >>>I am creating an online survey for one of our grants and one of the items
    >>>to be answered is county (U.S.). The county is to be in a drop-down menu
    >>>to prevent typos and counties from other states by mistake. I'm doing
    >>>server-s
    Ray Costanzo [MVP], Oct 8, 2004
    #4
  5. JayB

    Joker Guest

    I always had an extra option at the top of the list that was always
    selected that would contain the name of whatever was selected in the
    list. This did make the state listed twice, but it was easier then
    getting it listed only once.

    JayB wrote:

    > I am creating an online survey for one of our grants and one of the items to
    > be answered is county (U.S.). The county is to be in a drop-down menu to
    > prevent typos and counties from other states by mistake. I'm doing
    > server-side validation (many of our users have disabled JavaScript) and need
    > to retain the user's selection if they need to revisit the page to make
    > corrections.
    >
    > In the past I'd declare variables for each choice in the drop-down menu
    > (usually they are no more than five or six choices for what I've done) and
    > use a select case to set one of the variables to ' SELECTED'. However, in
    > the county list there are 86 choices.
    >
    > What would be the most efficient way to retain the users selection if they
    > need to go back to fix errors? I also am looking for a similar solution for
    > many checkboxes.
    >
    > Thanks,
    >


    --
    Please do not contact me directly or ask me to contact you directly for
    assistance.

    If your question is worth asking, it's worth posting.

    If it’s not worth posting you should have done a search on
    http://www.google.com/ http://www.google.com/grphp?hl=en&tab=wg&q= or
    http://news.google.com/froogle?hl=en&tab=nf&ned=us&q= before wasting our
    time.
    Joker, Oct 8, 2004
    #5
  6. JayB

    JayB Guest

    Ray, thanks for the suggestion.

    I put the counties in their own table and read them from the asp page. I use
    an if statement to determine if it's selected or not. Thanks again for your
    suggestion, it got me thinking on the right track.

    --
    JayB

    "Ray Costanzo [MVP]" <my first name at lane 34 dot commercial> wrote in
    message news:O$...
    > Well that kinda sucks... What you can do is:
    > <%
    > Dim s
    > s = Request.Form("dropdownName")
    > %>
    > <select name="dropdownName">
    > <option value="1"<% If s = "1" Then Response.Write " selected"%>>Option
    > 1</option>
    > <option value="2"<% If s = "2" Then Response.Write " selected"%>>Option
    > 2</option>
    > ...
    > </select>
    >
    > Ray at work
    >
    > "JayB" <> wrote in message
    > news:cly9d.8794$...
    >> Hard-coded HTML.
    >>
    >> --
    >> JayB
    >>
    >>
    >> "Ray Costanzo [MVP]" <my first name at lane 34 dot commercial> wrote in
    >> message news:%...
    >>> What does the code look like that you're using the display the dropdown
    >>> box? Do these values from a recordset, an array, just hard-coded ino the
    >>> HTML, something else?
    >>>
    >>> Ray at work
    >>>
    >>> "JayB" <> wrote in message
    >>> news:7Kx9d.8785$...
    >>>>I am creating an online survey for one of our grants and one of the
    >>>>items to be answered is county (U.S.). The county is to be in a
    >>>>drop-down menu to prevent typos and counties from other states by
    >>>>mistake. I'm doing server-s

    >
    >
    JayB, Oct 8, 2004
    #6
  7. JayB

    dlbjr Guest

    Most Effiecient:

    Since the items in the select box will not change (much), I would list the items in the global.asa
    with an Application variable like so:

    Application("STATES") = "AL,NY,TX,OK,GA"

    Use the following code in an ASP Page.

    Sub BuildStateDropDown(strDefault)
    With Response
    .Write "<select id='STATE' name='STATE'>"
    arData = Split(Application("STATES"),",")
    For i = 0 To UBound(arData)
    strItem = arData(i)
    .Write "<option value='"
    .Write strItem
    .Write "'"
    If strDefault = strItem Then
    .Write " SELECTED"
    End If
    .Write ">"
    .Write strItem
    .Write "</option>"
    Next
    .Write </select>"
    End With
    End Sub

    'dlbjr
    'Pleading sagacious indoctrination!
    dlbjr, Oct 8, 2004
    #7
  8. dlbjr wrote:
    > Most Effiecient:


    Doubtful, since multiple usage means multiple Splits. Not to mention the
    syntax error it will generate on the following line:

    > .Write </select>"




    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms. Please do not contact
    me directly or ask me to contact you directly for assistance. If your
    question is worth asking, it's worth posting.
    Dave Anderson, Oct 8, 2004
    #8
  9. JayB wrote:
    > What would be the most efficient way to retain the users selection if
    > they need to go back to fix errors? I also am looking for a similar
    > solution for many checkboxes.


    JScript makes it so easy. Stick this in one of your global includes:
    Array.prototype.toOptions = function(val) {
    for (var i=0,a=[]; i<this.length; i++) a.push(
    "<OPTION VALUE=\"" + this +
    (this==val ? "\" SELECTED>" : "\">") +
    this + "</OPTION>"
    )
    return a.join("")
    }

    Now ANY array can spit out a set of options. For example, an array of state
    codes:

    Define your array inline...
    var States = new Array("AK","AL","AR",...,"WY")

    ....or from a data source:
    for (var States=[]; !RS.EOF; RS.MoveNext())
    States.push(RS.Fields("State").Value)

    Call the method to generate the set of options:
    var StateList = States.toOptions(Request.Form("State").Item)

    Put it inline in your template:
    <SELECT NAME="State"><%=StateList%></SELECT>



    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms. Please do not contact
    me directly or ask me to contact you directly for assistance. If your
    question is worth asking, it's worth posting.
    Dave Anderson, Oct 8, 2004
    #9
    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.

Share This Page