ASP Quick Reference

ASP Quick Reference from CodeW3C.com. Print it out, put it in your pocket, for easy reference any time.

Basic Syntax

ASP scripts are enclosed by <% and %>. This is how content is output to the browser:

<html>
<body>
<% response.write("Hello World!") %>
</body>
</html>

The default language in ASP is VBScript. If you need to use another scripting language, please insert a language declaration at the top of the ASP page:

<%@ language="javascript" %>
<html>
<body>
<%
....
%>

Forms and User Input

Request.QueryString is used to collect values from a form with method="get". Information sent through GET from a form is visible to everyone (it will be displayed in the browser's address bar), and there is also a limit to the amount of data sent.

Request.Form is used to collect values from a form with method="post". Information sent through POST from a form is not visible to others and there is no limit to the amount of data sent.

ASP Cookies

Cookies are commonly used to identify users. Cookies are small files embedded by the server into the user's computer. Whenever the same computer requests a page through a browser, the cookie is also sent.

The Response.Cookies command is used to create cookies:

<%
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires="May 10,2012"
%>

Note: The Response.Cookies command must be located before the <html> tag!

The "Request.Cookies" command is used to retrieve cookie values:

<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>

File Reference

Through the #include directive, you can insert the content of one ASP file into another before the server executes it. The #include directive is used to create functions, headers, footers, or elements that are reused on multiple pages.

Syntax:

<!--#include virtual="somefile.inc"-->

Or

<!--#include file ="somefile.inc"-->

Please use the keyword virtual to indicate paths starting with a virtual directory. If a file named "header.inc" is located in a virtual directory named /html, the following code will insert the content of "header.inc":

<!-- #include virtual ="/html/header.inc" -->

Please use the keyword file to indicate relative paths. The relative path starts with the directory that contains the referenced file. If your file is located in the html directory, and the file "header.inc" is located in html\headers, the following code will insert the content of "header.inc" into your file:

<!-- #include file ="headers\header.inc" -->

Please use the keyword file with the syntax (..\) to reference files in higher-level directories.

Global.asa

The Global.asa file is an optional file that can contain object declarations, variables, and methods that can be accessed by each page in the ASP application.

Comment: The Global.asa file must be stored in the root directory of the ASP application, and each application can only have one Global.asa file.

The Global.asa file can only contain the following content:

  • Application Events
  • Session Events
  • <object> Declaration
  • TypeLibrary declaration
  • #include Directive

Application and Session Events

In Global.asa, you can specify what the application and session objects should do when the application/session starts and when it ends. The code to complete this task is located in the event handler.

Comment: When inserting code into the Global.asa file, we do not use <% and %>, we need to place procedures inside the HTML <script> tag:

<script language="vbscript" runat="server">
sub Application_OnStart
  ' some code
end sub
sub Application_OnEnd
  ' some code
end sub
sub Session_OnStart
  ' some code
end sub
sub Session_OnEnd
  ' some code
end sub
</script>

<object> Declaration

Using the <object> tag, you can also create objects with session or application scope in Global.asa.

Comment: The <object> tag should be placed outside of the <script> tag!

Syntax:

<object runat="server" scope="scope" id="id"
{progid="progID"|classid="classID"}>
......
</object>

TypeLibrary declaration

TypeLibrary is a container for the content of the DLL file corresponding to the COM object. By including a call to the TypeLibrary in the Global.asa file, you can access the constants of the COM object, and the ASP code can also report errors better. If your web application depends on COM objects that have declared data types in the typelib, you can declare the typelib in the Global.asa file.

Syntax:

<!--
METADATA TYPE="TypeLib"
file="filename"
uuid="typelibraryuuid"
version="versionnumber"
lcid="localeid"
-->

Session object

The Session object is used to store information about the user session or change its settings. The variables stored in the Session object contain information about a single user and can be accessed by all pages in an application.

Collection

  • Contents - Contains all entries appended to the session through script commands
  • StaticObjects - Contains all objects appended to the session using the HTML <object> tag
  • Contents.Remove(item/index) - Removes an item from the Contents collection
  • Contents.RemoveAll() - Removes all items from the Contents collection

Property

  • CodePage - Specifies the character set used to display dynamic content
  • LCID - Sets the regional identifier used to display dynamic content
  • SessionID - Returns the session id
  • Timeout - Sets or returns the timeout time of the session

Method

  • Abandon - Repeals all objects in the session object.

Application object

A set of ASP files that work together to complete a task is called an application. The Application object in ASP is used to bind these files together. All users share an Application object. The Application object should contain information used by many pages in the application (such as database connection information).

Collection

  • Contents - Contains all items appended to the application through script commands
  • StaticObjects - Contains all objects appended to the application using the HTML <object> tag
  • Contents.Remove - Removes an item from the Contents collection
  • Contents.RemoveAll - Removes all items from the Contents collection

Method

  • Lock - Prevents users from modifying variables in the Application object
  • Unlock - Allows users to modify variables in the Application object

Response object

The Response object is used to send output from the server to the user.

Collection

Cookies(name) - Sets the value of the cookie. If it does not exist, it creates a cookie and sets the specified value.

Property

  • Buffer - Specifies whether the output is buffered. When output caching is set, the server will prevent the response to the browser until all server scripts have been processed, or until the script calls Flush or End method. If this property is to be set, it should be located before the <html> tag in the .asp file.
  • CacheControl - Sets whether the proxy server can cache the output generated by ASP. If set to Public, the proxy server will cache the page.
  • Charset(charset_name) - Appends the name of the character set to the content-type header in the Response object.
  • ContentType - Sets the HTTP content type of the Response object. (For example, "text/html", "image/gif", "image/jpeg", "text/plain"). The default is "text/html"
  • Expires - Sets the time (in minutes) the browser cache is valid before the page expires
  • ExpiresAbsolute - Sets the date and time when the page cache on the browser expires
  • IsClientConnected - Indicates whether the client has disconnected from the server
  • Pics(pics_label) - Appends values to the PICS flag in the response header
  • Status - Specifies the value of the status line returned by the server

Method

  • AddHeader(name, value) - Adds new HTTP headers and values to the HTTP response
  • AppendToLog string - Adds a string to the end of the project's server log entry
  • BinaryWrite(data_to_write) - Writes data directly to the output without any character conversion
  • Clear - Clears buffered output. Use this method to handle errors. If Response.Buffer is not set to true, this method will produce a run-time error
  • End - Stops processing the script and returns the current result
  • Flush - Sends cached output immediately. If Response.Buffer is not set to true, this method will produce a run-time error
  • Redirect(url) - Redirects the user to another URL
  • Write(data_to_write) - Writes text to the user

Request Object

When a browser requests a page from a server, it is called a request. The request object is used to obtain information from the user.

Collection

  • ClientCertificate - Contains the values of fields stored in the client certificate
  • Cookies(name) - Contains cookie values
  • Form(element_name) - Contains form values. The form must be submitted using the POST method
  • QueryString(variable_name) - Contains the values of variables in the query string
  • ServerVariables(server_variable) - Contains the value of server variables

Property

  • TotalBytes - Returns the total number of bytes sent by the client in the request body

Method

  • BinaryRead - Retrieves data sent from the client to the server as part of a POST request

Server Object

The Server object is used to access properties and methods on the server.

Property

ScriptTimeout - Sets or returns how long a script can run before it is terminated.

Method

  • CreateObject(type_of_object) - Creates an instance of an object
  • Execute(path) - Executes another ASP file from within an ASP file. Control is returned to the original ASP file after the called ASP file has finished executing
  • ) - Returns an ASPError object that describes the error that occurred
  • HTMLEncode(string) - Applies HTML encoding to a string
  • MapPath(path) - Maps a relative or virtual path to a physical path
  • Transfer(path) - Send all status information to another file for processing. After transmission, control of the program will not return to the original ASP file
  • URLEncode(string) - Apply URL encoding rules to the string

Source: http://www.codew3c.com/asp/asp_quickref.asp