Drop Down Default Selected

J

JayB

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,
 
R

Ray Costanzo [MVP]

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
 
J

JayB

Hard-coded HTML.

--
JayB


Ray Costanzo said:
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
 
R

Ray Costanzo [MVP]

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
 
J

Joker

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

JayB

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

dlbjr

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!
 
D

Dave Anderson

dlbjr said:
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.
 
D

Dave Anderson

JayB said:
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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top