帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
Dynamic Date Drop-down Boxes
作者:By 发布时间:2005-03-12 来源:asphole.com
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-



  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·在ASP中使用数据库
·使用ASP脚本技术
·通过启动脚本来感受ASP的力量
·学习使用ASP对象和组件
·解析asp的脚本语言
·初看ASP-针对初学者
·ASP开发10条经验总结
·ASP之对象总结
·ASP与数据库应用(给初学者)
·关于学习ASP和编程的28个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统