Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop Down List Question 2

Status
Not open for further replies.

campbere

Technical User
Oct 10, 2000
146
US
I have a drop down box and when the page loads I have a value in it. I want to prevent a value from appearing until the user selects it, how is this done?

Here is the code that I wrote:

<td height=&quot;23&quot;><font face=&quot;Times New Roman&quot; size=&quot;3&quot; align=&quot;Left&quot;>
<select name=&quot;currentrecord&quot;>
<option>Y</option>
<option>N</option>
<option>Y/N</option>
</select>
</td>

thank you.
 
Add an empty selected option and give it a dummy value like -1. Just remember to check for -1 when the page submits.

Example:
Code:
<select name=&quot;currentrecord&quot;>
   <option value=&quot;-1&quot; selected>&nbsp;</option>
   <option value=&quot;Y&quot;>Y</option>
   <option value=&quot;N&quot;>N</option>
   <option value=&quot;Y/N&quot;>Y/N</option>
</select>
Wushutwist
 
I am not sure what you mean by checking for -1 when the page submits. Also comparing the two snippets of code I don't understand the use of value and selected in yours. Can you explain the use/need? I am new to ASP.


Your Example:

<select name=&quot;currentrecord&quot;>
<option value=&quot;-1&quot; selected> </option>
<option value=&quot;Y&quot;>Y</option>
<option value=&quot;N&quot;>N</option>
<option value=&quot;Y/N&quot;>Y/N</option>
</select>

MY Example:
<select name=&quot;currentrecord&quot;>
<option>Y</option>
<option>N</option>
<option>Y/N</option>
</select>

Thank you for your help.


 
in response to the empty select option, i would put the empty one at the bottom of the select list, just for appearances sake. for example:

<select name=&quot;currentrecord&quot;>
<option value=&quot;Y&quot;>Y</option>
<option value=&quot;N&quot;>N</option>
<option value=&quot;Y/N&quot;>Y/N</option>
<option value=&quot;-1&quot; selected> </option>
</select>


i will try to explain the value option in the select tag. if you don't include a value parameter, the form will return the text that appears in the dropdown the user selected, as you are probably aware. for example,

<select name='selState'>
<option>California</option>
<option>Oregon</option>
<option>Washington</option>

will return California, Oregon or Washington. by using the value parameter, the value is returned rather than the text. for example:

<select name='selState'>
<option value='CA'>California</option>
<option value='OR'>Oregon</option>
<option value='WA'>Washington</option>

if the user selects California, the value for selState will be 'CA'

the selected parameter tells the browser which item is the default item. by default, it's the first item in the list. in this example:

<select name='selState'>
<option value='CA'>California</option>
<option value='OR'>Oregon</option>
<option value='WA' selected>Washington</option>

the browser will show the dropdown with Washington selected.

hope this helps.
 
The value piece of the option tag is usually used to define an id for the data. If your select list is the fifty states then the id could be the order they were added to the union. The reason why I picked -1 as the value of the empty option is because typically the id also tend to be primary keys for a database tables and usually 1 and above. So -1 should never occur in valid data. If on the recieving ASP page checks for the select box value to be -1 you can effectively tell if someone left it blank. Example follows:

Submitting Page:
Code:
<select name=&quot;test&quot;>
   <option value=&quot;-1&quot; selected></option>
   <option value=&quot;1&quot;>DE</option>
</select>

Page being Submitted to:
Code:
<%
Dim test = request.form(&quot;test&quot;)
if test = &quot;-1&quot; then
   ' Nothing Selected
else
   ' Something Selected
end if
%>
Wushutwist
 
Thank you for all the responses everyone. The last think I don't understand is this checking the -1 when the page is submitted. I have never checked a value. What does this mean? Where does it happen? Can you provide me with an example?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top