Through the study of the first two articles, I believe that you have already gained a basic concept and overall impression of ASP's dynamic website design. Starting from this article, the author will start with the use of scripting languages and lead everyone to explore the true mysteries of ASP dynamic website design from shallow to deep.
Let me first learn some basic knowledge of applying scripting languages (mainly VBScript) in ASP.
Before you start learning scripting languages, you should understand some simple concepts - variables, processes. The so-called variable is a named storage location in computer memory, which contains data such as numbers or strings. It makes it easy for users to understand the name of script operations and provides users with a way to store, retrieve and operate data. A program is composed of one or more processes. In VBScript, a process is an instruction block. A process in the usual sense, such as Sub, is just for simple data processing.
In VBScript, strictly speaking, variables do not need to be declared.
like:
- <%Mystring=Thismystring%>
However, even if you do not need to declare variables before using them, you should develop a good habit of declaring variables when programming, as this helps prevent errors. Declaring a variable means telling the script engine that there is a variable with a specific name so that the variable can be referenced in the script. Declaring a variable in VBScript can use the Dim statement, as follows:
- <scriptlanguage=VBScript>
- <!--
- OptionExplicit' requires that all variables be declared in the script
- DimMystring
- Mystring=Thismystring
- -->
- </script>
The scope of a variable is the life period, which determines which script commands can access the variables. Variables declared inside the process have local scope. Each time the process is executed, the variable is created and then died. And no command outside the process can access it. Variables declared outside the process have a global scope and their values can be accessed and modified by any script command on the ASP page. When declaring a variable, local variables and global variables can have the same name. Changing one of the values does not change the other. If a variable is not declared, it may accidentally change the value of a global variable. For example, the following script command returns a value of 1, although there are two variables named Y:
- <%
- DimYY=1CallSetLocalVariableResponse.WriteY
- SubSetLocalVariable
- DimY
- Y=2EndSub%>
Since the variable is not explicitly declared, the following script command will return 2. When the procedure call sets Y to 2, the script engine considers that the procedure is to modify the global variable:
- <%
- Y=1CallSetLocalVariableResponse.WriteY
- SubSetLocalVariable
- Y=2
- EndSub%>
However, global variables are only available in a single ASP page, and to make it available outside of a single ASP page, you must assign a session or application scope to the variable. Session scope variables are available for all pages in the ASP application requested by a user. The same is true for application scope variables. For a single user, session variables are the best way to store information, such as user preferences, usernames, or user identification. For all users of a special application, application scope is the best way to store information, such as application-specific greetings or initial values required by the application. ASP provides two built-in objects to let you store variables: Session object and Application object, which will be discussed in future ASP built-in objects.
Let's look at the definition of a constant, which is used instead of a number or string name, and remains unchanged throughout the script. You can use the Const statement to create user-defined constants in VBScript. Use the Const statement to create string or numeric constants with certain meanings and assign them original values. For example:.
For example: < % Const mystring= This is a constant% >
< % Const myage=100 % >
Note that the string literal is contained between two quotes ( ). This is the most obvious way to distinguish between string-type constants and numerical constants. Date text and time text are contained between two pound signs (#). For example:
< % Const CutoffDate = #6-1-97# % >
After understanding constants and variables, let's take a look at what a process is. It is a set of script commands that can execute specified tasks and have return values. You can define your own procedures and then call them repeatedly in the script. You can place the process definition in the .asp file that calls the process, or you can place the general procedure in a shared .asp file and include it in the other .asp file that calls its procedures with the SSI #include directive. You can also choose another way to package these features in ActiveX components. Process definitions can appear inside the <SCRIPT> and <SCRIPT> tags and must follow the rules that declare scripting language. If the language used by the procedure is different from the main scripting language, use the <SCRIPT> element. Procedures in the main scripting language are separated by script delimiters (< % and % >). When tagging with HTML<SCRIPT>, two properties must be used to ensure that the server side can handle scripts. The syntax of using <SCRIPT> tag is as follows:
- <SCRIPTRUNAT=SERVERLANGUAGE=JSCRIPT>
- procedure definition
- </SCRIPT>
The RUNAT=SERVER property here notifies the web server to process scripts on the server. If this property is not set, the script will be processed by the client browser. The LANGUAGE property determines the script language used by this script block. You can specify any language with a scripting engine. Please use VBSCRIPT to specify VBScript; use JSCRIPT to specify JScript. If the LANGUAGE property is not set, the script block will be interpreted in the main script language.
In VBScript, processes are divided into two categories: Sub process and Function process. A Sub procedure is a set of VBScript statements contained between Sub and End Sub statements, performing operations but not returning values. Sub procedure can use parameters (constants, variables, or expressions passed by the calling procedure). If the Sub procedure does not have any parameters, the Sub statement must contain empty brackets().
The Function procedure is a set of VBScript statements that are included between the Function and End Function statements. The Function process is similar to the Sub process, but the Function process can return values. Function procedures can use parameters (constants, variables, or expressions passed by the calling procedures). If the Function procedure has no parameters, the Function statement must contain empty brackets(). Function The process returns a value through the function name, which is assigned to the function name in the statement of the process. Function The data type of the return value is always Variant. In the following example, the Sub procedure uses two inherent (or built-in) VBScript functions, namely MsgBox and InputBox, to prompt the user for information. The results calculated based on this information are then displayed. Calculation is done by the Function process created using VBScript, and the Celsius function converts Fahrenheit to Celsius. Sub procedure ConvertTemp When this function is called, a variable containing the parameter value is passed to the function. The conversion result is returned to the calling process and displayed in the message box.
- SubConvertTemp()
- temp=InputBox(Please enter Fahrenheit temperature.,1)
- The temperature of MsgBox is &Celsius(temp)&Celsius.
- EndSub
- FunctionCelsius(fDegrees)
- Celsius=(fDegrees-32)*5/9
- EndFunction
The way to pass data to the process is to use parameters. The parameter is used as a placeholder for the data to be passed to the process. The parameter name can be any valid variable name. When creating a process using a Sub statement or a Function statement, the process name must be followed by brackets. All parameters are included in brackets, separated by commas. For example, in the following example, fDegrees is a placeholder for the value passed to the Celsius function:
- FunctionCelsius(fDegrees)
- Celsius=(fDegrees-32)*5/9
- EndFunction
To get data from a process, the Function procedure must be used. Remember that the Function process can return values; the Sub process does not return values.
The above briefly introduces VBScript to you. Due to the length, it is impossible to introduce all the knowledge of VBScript in detail here. However, since ASP itself is not a programming language, in the process of writing ASP applications, we must use scripting language to implement many special functions. Therefore, we can flexibly master scripting language to write WEB applications using ASP It is crucial for the staff.
Whether you are a master with extensive programming experience or a beginner, as long as you now want to write WEB applications through ASP, the editor strongly recommends that you master at least one scripting language (such as VBScript).