ASP Syntax
You can not view ASP code in a browser, you will only see the output from ASP which is plain HTML. This is because the scripts are executed on the server before the result is sent to the browser.
In our school, every example displays the hidden ASP code. This will make it easier for you to understand how it works.
Examples
Write textHow to write some text into the body of the HTML document with ASP.
Text and HTML tagsHow to format the text with HTML tags.
The Basic Syntax Rule
An ASP file normally contains HTML tags, just as a standard HTML file. In addition, an ASP file can contain server scripts, surrounded by the delimiters <% and %>. Server scripts are executed on the server, and can contain any expressions, statements, procedures, or operators that are valid for the scripting language you use.
The Response Object
The Write method of the ASP Response Object is used to send content to the browser. For example, the following statement sends the text "Hello World" to the browser: Response.Write("Hello World").
VBScript
In ASP it is possible to use different scripting languages. The default language in ASP is VBScript, as in this example:
<%response.write("Hello World!")%>
The example above uses the response.write function to write Hello World! into the body of the HTML document.
JavaScript
To use JavaScript as the default scripting language, insert a language specification at the top of the page:
<%@ language="javascript" %><%Response.Write("Hello World!")%>
Note that - unlike VBScript - JavaScript is case sensitive. You will have to write your ASP code with uppercase letters and lowercase letters when the language requires it.
Other Scripting Languages
ASP comes with VBScript and JavaScript. If you want to script in another language, like PERL, REXX, or Python, you have to install scripting engines for them.
Because the scripts are executed on the server, the browser that requests the ASP file does not need to support scripting.
ASP Variables
A variable declared outside a procedure, can be changed by any script in the ASP file. A variable declared inside a procedure, is created and destroyed every time the procedure is executed.
Examples
Create a variableVariables are used to store information. This example demonstrates how to create a variable, assign a value to it, and insert the variable value into a text.
Create an arrayArrays are used to store a series of related data items. This example demonstrates how you can make an array that stores names.
Looping through headersThis example demonstrates how you can loop through the 6 headers in HTML.
Time-based greeting using VBScriptHow to write VBScript syntax in ASP. This example will display a different message to the user depending on the time on the server.
Time-based greeting using JavaScriptHow to write JavaScript syntax in ASP. This example is the same as the one above, but the syntax is different.
Lifetime of Variables
A variable declared outside a procedure, can be accessed and changed by any script in the ASP page in which it is declared.
A variable declared inside a procedure, is created and destroyed every time the procedure is executed. No scripts outside that specific procedure can access or change that variable.
To make a variable accessible to several ASP pages, declare it either as a session variable or as an application variable.
Session Variables
Session variables store information about one single user, and are available to all pages in one application. Common information stored in session variables are username and userid. To create a session variable, store it in a Session Object.
Application Variables
Application variables are also available to all pages in one application. Application variables are used to hold information about all users in a specific application. To create an application variable, store it in an Application Object.
ASP Procedures
In ASP you can call a JavaScript procedure from a VBScript and vice versa.
Examples
Call a procedure using VBScriptHow to call a VBScript procedure from ASP.
Call a procedure using JavaScriptHow to call a JavaScript procedure from ASP.
Call procedures using VBScriptHow to call a JavaScript procedure and a VBScript procedure from ASP.
Procedures
ASP code can contain procedures and functions:
<%sub vbproc(num1,num2)response.write(num1*num2)end sub%>The result of the calculation is: <%call vbproc(3,4)%>
Insert the <%@ language="language" %> line above the tag if you want to write procedures or functions in a scripting language other than the default:
<%@ language="javascript" %><%function jsproc(num1,num2){Response.Write(num1*num2)}%>The result of the calculation is: <%jsproc(3,4)%>
Friday, September 25, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment