Introduction to ASP-Microsoft version of the notes

Source: Internet
Author: User
Tags add array object definition empty variables version variable
Notes | microsoft | microsoft
ASP's "Processing instruction" <%@ keyword%> gives the information required by ASP to process. asp files. For example, the following instruction sets VBScript as the main scripting language for a page:
<%@ Language=vbscript%>
The processing instruction must be in the first line of the. asp file. To add more than one instruction to a page, the instructions must be in the same separator. Do not place processing instructions within the files that are included in the #include statement. You must add a space between the symbol @ and the keyword. The processing instruction has the following keywords:
LANGUAGE keyword sets the scripting language for. asp files. See Using scripting languages.
The EnableSessionState keyword specifies whether the. asp file uses a conversational sentence. See Managing Sessions.
The CODEPAGE keyword sets the code page (character encoding) of the. asp file.
The LCID keyword sets the location representation of the file.
The TRANSACTION keyword specifies that the. asp file will run in a transactional environment. See Understanding transactions.
Important: You can include multiple keywords in a single instruction. The keyword/value pairs must be separated by a space. Don't leave spaces on either side of the equals sign. The following example sets both a scripting language and a code page:
<%@ language= "JScript" codepage= "932"%>

When VBScript and ASP are used on the server, two VBScript features are disabled. Because scripts written using Active server Pages are executed on the server, VBScript statements InputBox and MsgBox that provide user interface elements are not supported. Also, do not use VBScript functions CreateObject and GetObject in server-side scripts. Instead, use Server.CreateObject so that the ASP can track the object routines. An object created by a CreateObject statement or GetObject statement can neither access an ASP-built object nor participate in a transaction.

Because all the scripts in the ASP are processed on the server side, it is not necessary to include HTML annotation tags to hide the script for browsers that do not support scripting, which is the usual practice of client script. All ASP commands are processed before the content is sent to the browser. You can use HTML comments to add notes to an HTML page that will be returned to the browser and will be visible if the user views the HTML source file. VBScript supports the annotation in apostrophe format. Unlike HTML annotations, these annotations are deleted when the script is processed and are not sent to the browser.

A comment cannot be included in an output expression. For example, the first line below works and the second row does not work because the line starts with <%=.
<% i = i +1 ' This statement is used to increment the I value. (This script will work.) )%>
<%= name ' This statement is used to print variable names. (This script will fail.) )%>

You can use the VBSCRIPT option Explicit statement in an. asp file to require that variables be explicitly declared using the Dim, Private, public, and REDIM statements. The option Explicit statement must precede any ASP commands, any HTML text, or script commands. For example:
<% Option Explicit%>
<HTML>
<%
Dim strUserName
Public lngAccountNumber
%>
Note: Limiting the scope of a variable to a procedure can improve performance.

It is necessary to develop the habit of explicitly declaring all variables. This is especially important when using the "#include" statement to include additional files in an. asp file. The included scripts are considered to be part of the containing file, although they are in a separate file. Unless you declare a variable, it is easy to forget that you must use a different variable name in the main script and the included script.

If you want to refer to the session scope variable more than once in your script, consider assigning the variable to a local variable to improve performance. Similarly, if the script repeats the reference to the application scope variable, you should assign the variable to a local variable to improve performance.

You can define your own constants. In VBScript, use the "Const" statement. In JScript, you can use the VAR statement to assign a constant value to a variable. If you want to use constants in multiple. asp files, you need to place the definition in a separate file and include these definitions in all. asp files that use the constant.

Distribute server-side script statements to client script (enclosed by HTML <SCRIPT> tags), which can dynamically initialize and change client script on request to configure the client database or DHTML personalization script. Creative use of this technology can also reduce the number of round-trip and server processing.

Use <SCRIPT> elements for the process of other languages other than the primary scripting language. When using HTML <SCRIPT> tags, you must use two properties to ensure that the server side is able to handle the script. The syntax for using the <SCRIPT> label is:
<script Language=jscript runat=server>
Process definition
</SCRIPT>
A command in a script block must form one or more procedures in the selected scripting language. For example, the following command defines the JScript procedure "MyFunction".
<HTML>
<script Language=jscript runat=server >
function MyFunction () {Response.Write () ("You called MyFunction ().") ") }
</SCRIPT>
Important: Do not include any script commands outside of the full process on the server side <SCRIPT> tags. Commands other than procedures do not have a fixed order of execution, and therefore can result in unpredictable results. In addition, you cannot use the ASP output command to <%=%> during the process. However, you can send content to the browser by Response.Write.

For VBScript, you can also use keyword call when calling procedures. However, if the calling procedure requires a variable, the list of variables must be enclosed in parentheses. If you omit the keyword call, you must also omit the parentheses that contain the list of variables. If any built-in function or user-defined function is invoked using the call statement, the return value of the function is discarded.

To pass the entire array to the procedure, in VBScript, the array name should be followed by a pair of empty parentheses.

Well-designed Web forms typically contain client script to validate user input before sending information to the server. Validation scripts can examine a number of things, such as whether a user input value is valid or if the text box is empty. You may need to verify that the user has entered numeric or textual information in the corresponding form field to prevent potentially invalid information from being sent to the server.
In general, it is good to validate form data as much as possible on the client side. In addition to prompt user input errors as soon as possible, client-side validation improves response speed, reduces server burden, and frees up bandwidth for other applications.
To improve interactivity, you can place the error message next to the form field that contains the invalid information so that the user finds where the error occurred. (Typically, web-based forms forward requests to a separate Web page that contains error messages.) Users who cannot immediately understand this information may lose heart. )

Because the number of items stored in the collection is not usually known, the ASP supports the Count property of the collection, which returns the number of items in the collection. You can use the Count property to specify the maximum value for a counter.
To make it more efficient to use the Count property with a "for" statement, you should assign the count value to a local variable and use the variable to set the maximum value for the counter. This way, the script engine does not have to look up the Count value each time it loops.
<%
' Declares a counter variable.
Dim Lngitem, Lngcount
Lngcount = Session.Contents.Count
' Repeat the loop until the counter in the collection equals the number of items.
For lngitem = 1 to Lngcount
Response.Write session.contents (Lngitem) & "<BR>"
Next
%>

A script can embed multiple association values in a single cookie to reduce the number of cookies passed between the browser and the WEB server. The Cookies collection of the Request and Response objects can contain multiple values in a single project. These subprojects or child keys can be used separately. Only Request.Cookies



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.