新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > Web开发 > Asp教程 > 正文:Dynamic Date Drop-down Boxes

Dynamic Date Drop-down Boxes

新客网 XKER.COM 2003-04-19 来源: 收藏本文
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 account 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-

收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐