Dynamic Date Drop-down Boxes

发表于:2007-06-30来源:作者:点击数: 标签:
By Kevin Perkins Article Article: One thing I‘’ve learned over the last few years is to use techniques in my applications that make it easy to debug and maintain, especially if you leave the project and have to come back to it at a later
By Kevin Perkins

Article

Article:

One thing I‘’ve learned over the last few years is to use techniques in my applications that make it easy to debug and maintain, especially if you leave the project and have to come back to it at a later time.

Besides error handling and commenting, I try to automate as much as possible to make maintenance a cake-walk. One area that can be tremendously automated is implementing date structures in your application.

For instance... if you‘’re building an ecommerce application, you‘’ll eventually have to deal with credit card expiry. The predominant technique for handling expiration dates is to use two drop-down boxes representing the month and the year. Having said that, it‘’s very easy to hard-code those dates in each drop-down box.... 1-12 for month, but what to what for the year? Do you use the current year (2000) and the following two (2001,2002) ?

What happens when we‘’re in year 2001? You‘’ll have an older entry for the previous year (2000), and you‘’ll be one short for the future (2002,?). This is bad practice because your application should be smart enough to remove previous years that are invalid to present-day transactions, as well as aclearcase/" target="_blank" >ccount for new credit cards that might have an expiration date farther in the future.

To work with drop-down dates dynamically, I determine the current month, current year, and how far into the future I want to account for expiration. My rule is 5 years, but each application is different:

<%@LANGUAGE=VBSCRIPT%>

<%
‘’ ----------------------------------------------------------------------------------
‘’ You could use similar looping techniques with JavaScript
‘’ ----------------------------------------------------------------------------------
%>

<html>
<head></head>
<body>

<form>

<%
‘’ ----------------------------------------------------------------------------------
‘’ Build the MONTH drop-down box
‘’ ----------------------------------------------------------------------------------

iMonth = Month(Now())
‘’ This produces the current month‘’s integer (ex. 7)
%>

<select name="drpMonth">
<% For i = 1 to 12 %>
<option <%
‘’ This will select the current month in the list
If CInt(iMonth = i) Then
Response.Write "selected "
End if
%>value="<%= i %>"><%= i %></option>
<% Next %>
</select>


<!-- Drop-down separator -->
/
<!-- End separator -->


<%
‘’ ----------------------------------------------------------------------------------
‘’ Build the YEAR drop-down box
‘’ ----------------------------------------------------------------------------------

iYear = Year(Now())
‘’ This produces the current year‘’s integer (ex. 2000)
%>


<select name="drpYear">
<% For i = iYear to ( CInt(iYear + 5) ) %>
<option <%
‘’ This will select the current year in the list
If CInt(iYear = i) Then
Response.Write "selected "
End if
%>value="<%= i %>"><%= i %></option>
<% Next %>
</select>


</form>

</body>
</html>



Of course, if you have more complex date interactions, you can manipulate the If/Then statement inside of the loop to select the appropriate value. And, if you‘’re working with dates in the past, you can reindex the starting value of (i) by subtracting how many years you want to go back...

Example:
"For i = iYear to ..." tells the application to do something "From 2000 to ..."
"For i = CInt(iYear - 2) to ..." tells the application to do something "From 1998 to ... "

-END-

原文转自:http://www.ltesting.net