Bilgisayar Bölümü ASP Kod ve Script Arşivi


Dynamic Date Drop-down Boxes

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 -->
&nbsp;/&nbsp;
<!-- 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-

"

ASP Kitabı

 

Faydalılar: Altın Fiyatları |Hediye Kolye | pırlanta fiyatları
Ana Sayfa  Sık Kullanılanlara Ekle   admin[at]bilgisayarbolumu.com | © 2006 - 2023 Bilgisayarbolumu.com