ASP Object
ASP Object
ASP Object
Objects are a way of encapsulating multiple methods (they're like functions) and variables in one easy to manage Uber-Variable (an Object). Objects in ASP resemble other Object Oriented Programming languages. In this lesson we will be using the ASP CDO.Message object as our example object to be dissected.
Advertise on Tizag.com
ASP Code:
<% Dim myObject Set myObject = Server.CreateObject("CDO.Message") 'You must Set your objects to "nothing" to free up the 'the computer memory that was allocated to it Set myObject = nothing %>
That wasn't too painful, was it? Let's cover some more bases on the object model. Objects are a collection of related things that are combined into this blob of programming goo that can be created and destroyed whenever we may need it. For example say that you wanted to make an object that allowed you to send an email... Well there are certain things all emails have: To, From, CC, Subject, etc. This list of variables that are common to every email would be quite tiresome to have to create for every email we sent. Wouldn't it be nice if we could create some sort of Uber-Variable(Object) that would group all these smaller variables into one thing?
objectName.propertyName = someValue
In this tiny example below we are creating a new mail object and setting its To and From properties.
ASP Code:
<% Dim myObject Set myObject = Server.CreateObject("CDO.Message") 'Then we set the To and From properties myObject.To = "little.timmy@example.com" myObject.From = "huge.jill@example.com" 'You must Set your objects to "nothing" to free up the 'the computer memory that was allocated to it Set myObject = nothing %>
Now I know we didn't DO anything in the above example, but we still need to learn a bit more about objects before we can get anything done! Objects, besides having a clump of associated common variables, may also have a collection of functions(which become referred to as methods) associated with them. These methods are processes that you would want to commonly do to either manipulate the variables of the object or to use the variables to do something. In our Messageobject we have a collection of information that, when put together into the proper email form and sent to an email service will become an email. All this complex code has been programmed by Microsoft employees and stored into the Message objects Send method.
ASP Code:
<% Dim myObject Set myObject = Server.CreateObject("CDO.Message") 'Then we set the To and From properties myObject.To = "little.timmy@example.com" myObject.From = "huge.jill@example.com" myObject.Subject = "Can you see me?" myObject.TextBody = "I'm really really big!" myObject.Send() 'You must Set your objects to "nothing" to free up the 'the computer memory that was allocated to it Set myObject = nothing %>
Note:If you are running this on your home computer there are a slough of issues that may arise with sending an email. Microsoft has a largeFAQ about using the CDO.Message object that may help you. Knowing Microsoft, that link may go dead soon, so Contact Us if it expires!
In this lesson you learned how to create and destroy an object in ASP. You also learned how to access the properties and utilize the methods of an object. If you still have no idea what this lesson was about, hopefully there's enough information for you to hack out what you need to do in your ASP project.
ASP Components
An ASP Server Component is a collection of code that has been made by Microsoft (advanced users can also create their own components), and included in IIS. With the use of ASP you can unlock the power of this pre-made code.
Advertise on Tizag.com
These objects can be used to do a ton of things, such as: an easy-to-use ad rotation service, an interface to a database, a means of manipulating files and much more.
Display:
Current folder is: tizagASP
Finishing Up
The last thing on our to-do list is to get access to the names of the files in our working directory. The Folder object contains a method that returns a collection of all the files in the current directory. The code for accessing this collection and displaying the filenames is in the example below.
If you have done every example in this ASP Tutorial your web page produced would look like this. Notice that the files are automatically sorted alphabetically!
Display:
ASP Comments
As we have stated way too many times now, ASP uses VBScript as its default programming language. This means VBScript comments are ASP comments. It's a darn shame too because that means there isn't any support for multiple line comments that you see in HTML and various other programming languages.
Advertise on Tizag.com
ASP Code:
<% 'Hello I am a comment Dim newVar1, newVar2
In the above example we had two comments. The first comment was a note style comment. Programmers often use these kinds of comments to leave information to people who are going to read their code or to themselves. The second comment we created commented out "Dim oldVar1, oldVar2" preventing the ASP interpreter from reading this line of code. This kind of comment is a block-out comment that is often used to temporarily remove code from a file that may be causing errors or is just unnecessary at the time.
ASP Code:
<% Dim newVar1, newVar2 newVar = "Hello. 'I am not commented out" %>
That's pretty ugly and confusing to look at, so let's get right in to the details of each of the symbols and see what they mean.
ASP Code:
<% Dim myString myString = "One String"
Display:
ASP Code:
<% 'This is a comment. %>
ASP Code:
<% Response.Write("This is probably the longest "&_ "string to be typed out on this page and maybe "&_ "even this whole tutorial on ASP!!!") %>
Display:
This is probably the longest string to be typed out on this page and maybe even this whole tutorial on ASP!!!
ASP Code:
<% Dim Dim Dim x=3 %> x y z : y=25 : z=x-y : y=x*z : z=x*x-z+y : y=5*3*z*2/x
myObject.MethodName()
If you would like to learn more about ASP objects see our ASP Object Lesson.
ASP Code:
<html> <body> <% 'My ASP code goes here. %> </body> </html>
ASP Code:
<%=2%> <br /> <%="Hello"%> <br /> <%=Date()%>
Display:
2 Hello 10/20/2011
Registering a DLL
Before you can start using the code that resides in the DLL file, you must first register the DLL with the windows runtime library. There are two ways to let windows know about this new DLL.
or
The first option is self-explanatory, but the second option takes a bit more work. Here is a quick HowTo on registering DLLs: 1. 2. 3. 4. Find out the location of your DLL. We will be using C:\Inetpub\wwwroot\tizagASP\myDLL.dll in this example. Click on the Start menu and choose Run Type in regsvr32.exe "C:\Inetpub\wwwroot\tizagASP\myDLL.dll" (With the quotes) You should get a confirmation message if it succeeded, such as: "DllRegisterServer in C:\Inetpub\wwwroot\tizagASP\myDLL.dll succeeded"
ASP Code:
<% 'Note this is example code, it will not work ' unless you create a myDLL, myClass, and a myMethod Dim myObject myObject = Server.CreateObject("myDLL.myClass") myObject.myMethod("something") myObject = nothing %>
ASP ADO
This lesson will provide a brief overview of what ADO is and why it is necessary to have in your ASP programming repertoire. ADO stands for ActiveX Data Objects. ActiveX Data Objects are a collection of components that can be used in your ASP programs.
Advertise on Tizag.com
Before you can connect to the Access database you have to first create the Access database file. Fire up your copy of MS Access and create the following database: 1. 2. 3. 4. 5. 6. 7. 8. Create a New blank database called "tizag.mdb" (without the quotes) and save it to "C:\Inetpub\wwwroot\tizagASP\" (without the quotes) Use the wizard to create the database. Add two fields to your table: FirstName and LastName Click Next Name the table "TizagTable" (without the quotes) Select "Yes, set a primary key for me" Click Next Click Finish
Now that we have our database all that remains to connect to it.
ASP Code:
<% Dim myConn Set myConn = Server.CreateObject("ADODB.Connection") myConn.Open = ("DRIVER={Microsoft Access" &_ " Driver (*.mdb)};DBQ=" &_ "C:\Inetpub\wwwroot\tizagASP\tizag.mdb;") myConn.Close() Set myConn = nothing %>
ASP vs PHP
Here at Tizag.com we teach two ways to program dynamic web pages: ASP and PHP. Which one is right for you? Which one should you spend your precious time and resources learning? This lesson will talk about the benefits and drawbacks of both of these technologies and try to give you the direction you need for choosing one technology over the other.
Advertise on Tizag.com
However, businesses do not readily embrace PHP for many reasons. A great deal of companies are running operating systems such as Windows Server 2003 or one of the Window NTs, which have been optimized to run Microsoft's proprietary language ASP. Companies usually are reluctant to switch technologies when they already have a history with one type of technology. Such a transition requires retraining or even retraining much of their staff.
ASP Files
All file interactions in ASP are done through the File System Component that is included with IIS. It includes many objects that give you a window into your system's file system. Some of the more important objects include:
Advertise on Tizag.com
1. 2. 3.
Before you can gain access the second and third objects we listed above you must create the grand daddy master File System Object (FSO). Through this overarching object, everything in the File System Component can be accessed.
ASP Code:
<% Dim myFSO Set myFSO = Server.CreateObject _ ("Scripting.FileSystemObject") Set myFSO = nothing %>
ASP Code:
<% Dim myFSO, myFO Set myFSO = Server.CreateObject _ ("Scripting.FileSystemObject") Set myFO = myFSO.GetFile _ ("C:\Inetpub\wwwroot\tizagASP\firstscript.asp") Response.Write("The filename is: " & myFO.Name) Set myFO = nothing Set myFSO = nothing %>
Display:
the Name property for both the Folder and File objects.
ASP Code:
<% Dim myFSO, myFolderO Set myFSO = Server.CreateObject("Scripting.FileSystemObject") Set myFolderO = _ myFSO.GetFolder("C:\Inetpub\wwwroot\tizagASP") Response.Write("Current folder is: " & myFolderO.Name) For Each fileItem In myFolderO.Files Response.Write("<br />" & fileItem.Name) Next Set myFolderO = nothing Set myFSO = nothing %>
If you have been following along with this tutorial start to finish your browser will display something like this when you execute the script.
Display:
The filename is: tizagASP firtscript.asp tizag.mdb tizagComponent.asp tizagEmail.asp tizagForm.html tizagGet.asp tizagPost.asp That's all we have for files in ASP for the time being. Look for a full coverage of ASP Files in the near future.
ASP Dates
This lesson will teach you how to use the ASP Date Function, how to convert an ASP Date to a String and how to format an ASP Date.
Advertise on Tizag.com
ASP Code:
<% Response.Write(Date()) %>
Display:
10/20/2011 You can even use some shorthand techniques to make printing out the current date to your web page only one line of ASP Code. See our ASP Special Characters Lesson for more information.
ASP Code:
<%=Date%>
Display:
10/20/2011 Pretty sweet.
However, if you want to use ASP to format a date into a specific form other than the default format of DD/MM/YYYY (D = day, M = Month, Y = Year) then you will need to use the FormatDateString function. This function is covered in the next section. If you want to convert a string to date format, check out our String to Date lesson.
FormatDateTime Function
The Format Date Time function takes two arguments: a date and (optional) an integer from 0 through 4. The meanings of these numbers are as follows:
0 - This is the default setting. A short date DD/MM/YYYY will be used. 1 - A long date defined by the computer's regional settings. 2 - A short date defined by the regional settings. 3 - (time)A time using the time format defined by the regional settings. 4 - (time)A time using military time HH:MM (H = hour, M = Minute)
ASP Code:
<% Response.Write("0 = " & FormatDateTime(Date, 0)) Response.Write("<br />1 = " & FormatDateTime(Date, Response.Write("<br />2 = " & FormatDateTime(Date, Response.Write("<br />3 = " & FormatDateTime(Date, Response.Write("<br />4 = " & FormatDateTime(Date, %> 1)) 2)) 3)) 4))
Display:
0 1 2 3 4 = 10/20/2011 = Friday, October 21, 2005 = 10/20/2011 = 12:00:00 AM = 00:00
You'll notice that the last two options are pretty worthless if you are just working with a standard date, but they are useful for formatting ASP's Time.
FreeSQL - This web site allows developers to practice their SQL for free. This is not a web host. DiscountASP.net - Cheap SQL Hosting. This is a shared web hosting environment.
Fortune City - Extra features for small businesses. Aschosting - 24 hour toll free emergency support line.
ICO - Dedicated Server Solutions. The Planet - Another option for a dedicated solution.
FreeSQL - This web site allows developers to practice their SQL for free. This is not a web host. Think Host - Cheap MySQL Hosting. This is a shared web hosting environment.
ASP Manual(s)
On this page we have collected a bunch of useful sites that provide a manual to ASP and VBScript, the language that you program ASP in by default. This page itself is not a manual. ASP is proprietary software of Microsoft and so we have split up information into Microsoft and Non-Microsoft related resources.
Advertise on Tizag.com
Microsoft
MSDN ASP Tutorial - Microsoft's ASP Tutorial under Windows 2000 Server Documentation Online. ASP to ASP.NET - This manual provides information on migrating from ASP to ASP.NET MSDN VBScript User Guide - Microsoft's VBScript User Guide. If you're going to be programming in
ASP you might want to get a strong grasp of VBScript, the default language to program ASP in.
Non-Microsoft
ASPIN - ASPIN has a huge collection of resources for ASP. The only downside is the time and
The result is mypage.asp?message=Hello%20World%21 Decoding the querystring is easy - just call unescape() function in JavaScript. VBScript, on the other hand, does not have any in-built function to decode URLStrings. Note: You will need to call unescape() within the same page as you called escape() to decode the strings. Otherwise, a simple Request.QueryString("message") will automatically decode the URL for you. I wrote this tiny function so you can include it on any ASP page with any scripting language and use it. Just include it in any ASP page and call the function URLEncode() with a string argument. Listing 5 <script language=JavaScript RUNAT=SERVER> // This function decodes the any string // that's been encoded using URL encoding technique function URLDecode(psEncodeString) { return unescape(psEncodeString); } </script> An example of using this is in Listing 6 Listing 6 <%=URLDecode("Hello%20World%21") %> the result being - Hello World! Note: Some people have asked me how to decode those "+"s, which are nothing but encoded spaces in the URL. Although Request.QueryString("your_key") should remove the +s, here is the code that will replace +s with spaces. This is nothing but a modified Listing 5. Listing 6 <script language=JavaScript RUNAT=SERVER> // This function decodes the any string // that's been encoded using URL encoding technique function URLDecode(psEncodeString) { // Create a regular expression to search all +s in the string var lsRegExp = /\+/g; // Return the decoded string return unescape(String(psEncodeString).replace(lsRegExp, " ")); } </script>