Go Back   Webforumz.com > Blogs > Monie

Notices



ASP Programming Tips and Technique

Posted Oct 26th, 2007 at 06:37 by Monie
Updated Nov 1st, 2007 at 08:05 by Monie
[ Option Explicit ]
  • <% Option Explicit %>
  • It is usually placed at the top of your asp page.
  • Option Explicit requires that all variable names be declared (with the Dim statement). Using Option Explicit to force variable declaration is good for a number of reasons.
  • Using Option Explicit forces you to declare all your variables before you use them. If you use a variable you haven't declared then an error will be generated and it will be easier for your debugging process.

[ On Error resume Next ]
  • <% On Error resume Next%>
  • It is usually placed at the top of your asp page.
  • Easily trap and turn nasty error messages into easy on the eye messages that the user can understand.
  • Example:
<%
If Err.number <> 0 Then 'if there is an error

Response.Write ("Sorry you have encoutered an error." & "<br>")
Response.Write
("Your error number is " & Err.number & "<br>")

Response.Write
("The error source is " & Err.source & "<br>")

Response.Write
("The error description is " & Err.description & "<br>")

End If
%>

or you could redirect the user to a new page that contains all your custom made error message:

<%
If Err.number <> 0 Then 'if there is an error

Response.
Redirect (my_error_message_page.asp)
End If
%>

[ ASP Formatting Dates and Times ]
First, declare your variable as follow:

<%
Dim strNow
strNow = Now()
%>


and you can start manipulating the date and time..
  • <%= Date() %> prints out 26/10/2007
  • <%= Now() %> prints out 26/10/2007 08:13:51
  • <%= Time() %> prints out 08:13:51
  • <%= FormatDateTime(strNow, vbGeneralDate) %> prints out 26/10/2007
  • <%= FormatDateTime(strNow, vbLongDate) %> prints out 26 October 2007
  • <%= FormatDateTime(strNow,vbShortDate) %> prints out 26/10/2007
  • <%= FormatDateTime(strNow, vbLongTime) %> prints out 08:13:51
  • <%= FormatDateTime(strNow, vbShortTime) %> prints out 08:13
  • <%= Year(strNow) %> prints out 2007
  • <%= Month(strNow) %> prints out 10
  • <%=Day(strNow) %> prints out 26
  • <%=Hour(strNow) %> prints out 8
  • <%=Minute(strNow) %> prints out 13
  • <%=Second(strNow) %> prints out 51
  • <%= WeekDay(strNow) %> prints out 6
  • <%= WeekDayName(WeekDay(strNow)) %> prints out Friday
  • <%= WeekDayName(WeekDay(strNow), 1) %> prints out Fri

[ How to stop an ASP page Caching ]
Everytime a page is loaded into the browser, that page will be loaded into the browser's cache. We don't want pages cached because if they are, credit card numbers, addresses and all kinds of stuff can be gathered from the cached page. How to stop an ASP page from being cached by the browser? Put this code on top of the pages that you want not to be cache by the browser.

<%
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
%>

Now, your page is safe!



Total Comments 0

Comments

 
Recent Blog Entries by Monie

All times are GMT. The time now is 21:09.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43