Through the first two articles, I believe you have gained a basic idea and an overall impression on ASP dynamic website design. Starting from the beginning of this article, I will start with the use of the scripting language and start to explore the real mysteries of ASP dynamic website design from a simple perspective.
After the second article was published, many friends sent me "Mei Er", hoping to see the third, fourth, and even fourth sections of this article as soon as possible ...... Or even a friend anxiously asked me to send him the full text of ASP. I am excited by seeing so many like-minded friends. Although ASP was launched two years ago, it was not until this year that it received the attention of Chinese people for its flexible and convenient development process and good web database connection functions, however, due to the lack of detailed teaching materials on ASP Web application development in China, the majority of Web developers in China are still in the closed-door e-text stage, including the author himself. Because of this, the author has read the idea of writing articles. Only after receiving the strong support from Mr. Weng bin from chinabyte network college can you see this article.
The author hopes that this article will help Web developers and enthusiasts to participate in ASP learning and communication, to meet the needs of readers at different levels, the author decided to start with the compilation of the most basic scripting language, next, I will explain the ASP built-in objects, ActiveX components, and examples of developing Web applications using ASP. I believe that through continuous learning for a period of time, in less than a few months, you will be able to develop your own dynamic website with ease. Next, let's take a look at some basic knowledge of using the scripting language (mainly VBScript) in ASP.
Before learning the scripting language, you should understand some simple concepts-variables and processes. A variable is a named storage location in the computer memory, which contains numbers, strings, and other data. It makes it easy for users to understand the name of a script operation, it 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 a "command block". Generally, a process, such as Sub, is only used for simple data processing.
In VBScript, variables do not need to be declared strictly,
For example: <% mystring = "this is my string" %>
However, even if you do not need to declare variables before using them, you should also develop a good habit of declaring variables during programming, because this will help prevent errors. Declaring a variable indicates that the script engine has a variable with a specific name, so that the variable can be referenced in the script. To declare a variable in VBScript, you can use the "dim" statement as follows:
<Script language = "VBScript">
<! --
Option explicit requires that all variables be declared in the script
Dim mystring
Mystring = "this is my string"
-->
</SCRIPT>
The scope of the variable is the life cycle. It determines which script commands can access the variable. Variables declared in the process have local scopes. Every time a process is executed, the variable is created and then extinct. Any command outside the process cannot access it. Variables declared outside the process have a global scope, and their values can be accessed and modified by any script commands on the ASP page. When declaring a variable, the local variable and global variable can have the same name. Changing one value does not change the other value. If no variables are declared, you may accidentally change the value of a global variable. For example, the following script command returns value 1, although there are two variables named Y:
<%
Dim YY = 1 call setlocalvariableresponse. Write y
Sub setlocalvariable
Dim y
Y = 2end sub %>
Because the variables are not explicitly declared, the following script command returns 2. When the process call sets y to 2, the script engine considers that the process is to modify the global variable:
<%
Y = 1 call setlocalvariableresponse. Write y
Sub setlocalvariable
Y = 2
End sub %>
However, global variables are only available on a single ASP page. To make them available outside a single ASP page, you must assign the variables a session or application scope. Session scope variables are available for all pages in the ASP application requested by a user. The same applies to Application Scope variables. For a single user, session variables are the best way to store information, such as user preferences, user names, or user identities. For all users of a special application, the application scope is the best way to store information, such as the initial values required by an application-specific greeting or application. ASP provides two built-in objects for you to store variables: Session objects and application objects, which will be discussed in future ASP built-in objects.
Let's take a look at the definition of constants. constants are used to replace the name of a number or string and remain unchanged throughout the script. You can use the const statement to create user-defined constants in VBScript. Using the const statement, you can create string or numeric constants with specific meanings of names and assign them original values. For example :.
For example: <% const mystring = "this is a constant" %>
<% Const myage = 100%>
Note that the string text is contained between two quotation marks. This is the most obvious way to distinguish between string constants and numeric constants. The date text and the time text are included in the interval between two periods. 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 group of script commands that can execute specified tasks and have returned values. You can define your own processes and call them repeatedly in the script. You can put the process definition in the call process. the common process can also be stored in a shared ASP file. ASP file, and then use the SSI # include command to include it into other calls to its process. ASP file. You can also choose to package these functions in ActiveX components. The process definition can appear in the <SCRIPT> and </SCRIPT> labels and must follow the rules for declaring the script language. If the language used in the process is different from the main script language, use the <SCRIPT> element. The processes in the main script language are separated by the script delimiters (<% and %>. When HTML <SCRIPT> is used, two attributes must be used to ensure that the server can process scripts. The syntax marked with <SCRIPT> is as follows:
<SCRIPT runat = server Language = JScript>
Procedure Definition
</SCRIPT>
Here, the runat = server attribute notifies the Web server to process scripts on the server. If this attribute is not set, the script will be processed by the client browser. The language attribute determines the script language used by the script block. You can specify any language with a script engine. Use Vbscript to specify VBScript; Use JScript to specify JScript. If the language attribute is not set, the script block is interpreted in the main script language.
In VBScript, the process is divided into two types: sub process and function process. The sub process is a set of VBScript statements between the sub and the end sub statements. The execution operation does not return values. Sub processes can use parameters (constants, variables, or expressions passed by the call process ). If the sub process has no parameters, the sub statement must contain empty parentheses ().
The function process is a set of VBScript statements between the function and end function statements. The function process is similar to the sub process, but the function process can return values. A function process can use parameters (constants, variables, or expressions passed by the call process ). If the function process has no parameters, the function statement must contain empty parentheses (). The function process returns a value through the function name, which is assigned to the function name in the process statement. The data type returned by function is always variant. In the following example, the sub process uses two inherent (or built-in) VBScript functions, msgbox and inputbox, to prompt users to enter information. The calculation result is displayed based on the information. The calculation is completed by the function process created using VBScript. The Celsius function converts degrees Fahrenheit to degrees Celsius. When the sub process converttemp calls this function, variables containing parameter values are passed to the function. The conversion result is returned to the call process and displayed in the message box.
Sub converttemp ()
Temp = inputbox ("Enter the Fahrenheit temperature. ", 1)
Msgbox "temperature is" & Celsius (temp) & "Celsius. "
End sub
Function Celsius (fdegrees)
Celsius = (fdegrees-32) * 5/9
End Function
A parameter is used to transmit data to a process. 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 using a sub or function statement to create a process, the process name must be followed by parentheses. All parameters are included in the brackets, which are separated by commas. For example, in the following example, fdegrees is a placeholder for the value passed to the Celsius function:
Function Celsius (fdegrees)
Celsius = (fdegrees-32) * 5/9
End Function
To obtain data from a process, you must use the function process. Remember that the function process can return values; the sub process does not return values.
The above section briefly introduces VBScript. Due to the length, it is impossible for the author to introduce all the knowledge of VBScript here. However, ASP is not a programming language. Therefore, many special functions must be implemented by scripting during ASP application programming, therefore, flexible and proficient scripting is crucial for a programmer who uses ASP to write web applications. Whether you are a master with rich programming experience or a beginner, as long as you want to write Web applications through ASP, the author strongly recommends that you master at least one scripting language (such as VBScript ). At the end of this article, I will leave a homework assignment for you. I hope you can learn about the relevant books and learn the script language quickly in practice.
Homework after class:
The author is using ASP to create a web-based BBS system. The author hopes to add a special function in it, that is, after logging on to the BBS, any user can view all newly released information in the last seven days. Because the built-in objects and components of ASP do not provide this function, the author invites you to design such a function from VBScript. If you have any questions, please email the author and the answer will be published in the next article. Stay tuned.