Web Application Development Advice & How To
Active Server Pages Development Advice & How To
 
Shining Star Services
Articles
ASP.NET
ASP.net Articles
ASP, DHTML, HTML
HTML Tutorial
Auto-select an Element from a Menu or Scrolling List & Save Keystrokes!
ASP Driven DHTML Slider Menus REVISITED One Year Later! Part II
ASP Driven DHTML Slider Menus
ASP Driven HTML Outlines
Reusing Code with ASP Include Files and Subroutines
Security
Hacker Query Check
.htr IIS Security Issue
Databases, Cookies
Functions to Open a Database Connection and Record Set
Setting Up and Using OraSession to Manage Your Oracle Database Objects
Storing Non-Durable Data for Cookie-less Sessions
Javascript
Smart Popups
Javascript: Validate Numeric Fields
Javascript Confirm Form Submission
Javascript Dynamic Text Area Counter
Javascript: Check All and Uncheck All Check Boxes
Javascript Field Validations -- Client Side Scripting
Miscellaneous
Tree Select Demo
Adobe Extension Manager
Scandisk & Defrag Pointers
Standards & Style
Setting Up Your Own ASP Development Templates
Creating a Project Template for Estimations of Time, Tasks, and Resources
To Host or Not To Host
ASP Naming Conventions
HTML Naming Conventions & Visual Interdev HTML Generation
Working with and in spite of the Visual Interdev Design Mode
Commenting Your ASP Source Code
Letters!
Reader Letters

Articles Home
Shining Star Services

Aspin.com
VisualBuilder ASP Tutorial
DevDex
   AS SEEN ON
ASP Wire

 
Auto-select an Element from a Menu or Scrolling List & Save Keystrokes!
By Nannette Thacker - 08/23/2000

The above form may not look like much, but with my "fmtSelectItem()" function, I created my <select> form element scrolling list quickly with very little typing -- AND automatically defaulted to the appropriate selection in the list.

For instance, in your customer table, you know your customer's favorite fruit is "Bananas" by looking it up in the table. Pass that value into the function, and it will automatically compare the customer's favorite fruit to the value in the option tag. If it matches, it will add "selected " to the option tag -- saving you from a lot of keystrokes!

Here is the ASP code to generate the form:

<form action="/articles/articles/misc/select.asp?ID=<%=siteID%>"
	method="POST" id="form1" name="form1">
<select NAME="mylist" size="4">
<%
	dim mylist
	mylist = Request.form("mylist")
	if mylist = "" then
		' set default
		mylist = "Bananas"
	end if
	call fmtSelectItem("Apples",mylist)
	call fmtSelectItem("Oranges",mylist)
	call fmtSelectItem("Grapes",mylist)
	call fmtSelectItem("Pears",mylist)
	call fmtSelectItem("Cherries",mylist)
	call fmtSelectItem("Tangerines",mylist)
	call fmtSelectItem("Bananas",mylist) %></select>
<input type="Submit" name="Submit" value="Select">
</form>

In this page's example, I use the request.form to populate the default value. If it is empty, I set the default to "Bananas." However, in reality, you would probably use a value from a table record, and the form would likely not just continually call itself. :)

But notice how simple it was for me to create the option tag for each element in the scrolling list. All I had to do was type (or cut and paste) this several times, replacing "Apples" with the appropriate fruit:

	call fmtSelectItem("Apples",mylist) 
Your code as presented to the browser now looks like this:

<form action="select.asp" method="POST" id=form1 name=form1>
<select NAME="mylist" size="4">
<option value="Apples">Apples</option>
<option value="Oranges">Oranges</option>
<option value="Grapes">Grapes</option>
<option value="Pears">Pears</option>
<option value="Cherries">Cherries</option>
<option value="Tangerines">Tangerines</option>
<option selected value="Bananas">Bananas</option>
</select>
<input type="Submit" name="Submit" value="Select"> 
</form>     
And here's the function that does all the work:

<%
' Nannette Thacker library
' http://www.shiningstar.net
' select the item from a list
' based on the value passed in
' pass the value to display and the value to select
Function fmtSelectItem(sItem,myItem)
	Dim strOutput
	strOutput = "<option "
	if myItem = sItem then
		strOutput = strOutput & "selected "
	end if
	strOutput = strOutput & "value=""" & sItem & """>"
	strOutput = strOutput & sItem
	strOutput = strOutput & "</option>" & vbCrLf
	Response.Write strOutput
End Function
%>

In the above function, you've passed in the "sItem" or the Fruit Name to be displayed in the list and to be used as the "value" of the option.

You've also passed in "myItem" which is the value you are comparing against the value of the current element in the list. If the value of "myItem" is equal to the value of "sItem" then you add "selected" to the option tag.

This function can also be used when creating your list of options by traversing a table record set. For instance your list may be populated from a table of zip codes.

This can also be used for a list of States or Provinces and you wish to pre-select the user's State. However, I would rewrite the function to pass in 3 values.

  1. The State Name -- as the Name to Display. (sItem)
  2. The 2 letter State Abbreviation -- as the "value" of the option. (newfield)
  3. The User's 2 letter abbreviated State stored in his/her record. ("myItem" compared against "newfield")





ChristianSinglesDating

ShiningStar.net | ShiningStarSingles.com | Christian911.com