Fourth ASP Scripting Basics

Source: Internet
Author: User
Tags definition constant empty include numeric variables variable web database
Through the first two of the study, I believe that you have an ASP dynamic website design has a basic concept and the overall impression. From the beginning of this article, the author will start from the use of scripting language, to lead us to explore the real secret of ASP dynamic website design.

After the publication of the second article, a lot of friends sent me a "sister son", hoping to see the third, four, and even the first article ... , even a friend anxiously asked me to send the full text of the ASP article to him. Seeing so many like-minded friends makes me feel great. Although ASP had been launched two years earlier, but it was not until this year that it's a flexible and convenient development process, good WEB database connectivity has been the attention of the people, however, due to the lack of domestic development of ASP Web application of detailed teaching materials, so that the vast number of domestic web developers are still in a closed door gnawing The stage of E-text also includes the author himself. Because of this, the author Caimont The idea of writing an article, after receiving the strong support of Mr. Hongbin of Chinabyte Network College, you will be able to see this article.

The author earnestly hopes that through this article for the vast number of WEB developers and enthusiasts to provide convenience, to enable everyone to participate in the study and exchange of ASP, in order to cover the needs of readers at different levels the author decided to start from the most basic scripting language, and then step-by-step to explain the ASP built-in objects, ActiveX Components and the use of ASP to develop a WEB application example, I believe that through a period of continuous learning, within a few months you will be able to easily develop their own dynamic Web site. Please follow me to learn some of the basics of using scripting languages (mainly VBScript) in ASP.

Before you begin to learn the scripting language, you should understand some simple concepts--variables, processes. A variable is a named storage location in the computer's memory that contains data such as numbers or strings, which makes it easy for users to understand the name of the script operation and provides a way for the user to store, retrieve, and manipulate the data. A program is made up of one or more processes, and in VBScript, a process is a "command block," usually a process such as a Sub, just for simple data processing.
In VBScript, a variable is strictly not declared,

such as:<% mystring= "This are my string"% >

However, even if you don't need to declare a variable before you use a variable, you should develop a good habit of declaring variables at programming time, because it helps prevent errors from happening. Declaring a variable means telling the script engine that there is a variable of a particular name, so that the variable can be referenced in the script. Declaring a variable in VBScript allows you to use the Dim statement as follows:
< script language= "VBScript" >
<!--
Option Explicit ' requires that all variables be declared in the script
Dim Mystring
Mystring= "This are my string"
-->
</script>

The scope of the variable, which is the lifetime, determines which script commands can access the variable. Variables declared within a procedure have local scope. Each time a procedure is executed, the variable is created and then extinct. None of the commands outside the procedure can access it. Variables declared outside the procedure have global scope, and their values can be accessed and modified by any script command on the ASP page. Local variables and global variables can have the same name when declaring a variable. Changing one of the values does not change the value of the other. If you do not declare a variable, you may inadvertently change the value of a global variable. For example, the following script command returns a value of 1, although there are two named Y variables:
<%
Dim YY = 1Call setlocalvariableresponse.write Y
Sub setlocalvariable
Dim Y
Y = 2End Sub% >

Because the variable is not explicitly declared, the following script command returns 2. When the procedure call sets Y to 2 o'clock, the script engine considers the procedure to be modifying a global variable:
<%
y = 1Call Setlocalvariableresponse.write y
Sub setlocalvariable
Y = 2
End sub% >

However, global variables are available only in a single ASP page, and to make it available outside a single ASP page, you must give the variable a session or application scope. Session scope variables are available to all pages in the ASP application that a user requests. 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, user names, or the identity of a user. For all users of a particular application, the application scope is the best way to store information, such as application-specific greetings or the initial values required by the application. ASP provides two built-in objects to store variables: Session objects and application objects, which will be highlighted in future ASP-built objects.

Let's take a look at the definition of a constant, which is used instead of a number or string of names, and it stays the same throughout the script. You can use the Const statement to create user-defined constants in VBScript. Use the Const statement to create a string or numeric constant with a certain meaning to the name, and to assign them a literal value. For example:.
such as:<% 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 differentiate between string and numeric constants. The date text and time text are contained between two pound numbers (#). For example:
<% Const Cutoffdate = #6 -1-97#% >

After understanding the constants and variables, let's look at what the process is. It is a set of script commands that can perform a specified task and have a return value. You can define your own procedures and then call them repeatedly in your scripts. You can place a procedure definition in an. asp file of the calling procedure, or you can place a common procedure in a shared. asp file, and then include it in an SSI #include directive to another. asp file that calls its procedure. You can also choose another way to package these features in an ActiveX component. Procedure definitions can appear inside < script > and </SCRIPT > tags and must follow the rules for declaring scripting languages. If the procedure uses a different language than the main scripting language, use the < script > element. Procedures in the main scripting language are delimited by script delimiters (<% and% >). When you use the html< script > tag, you must use two properties to ensure that the server side can handle the script. The syntax for using the < SCRIPT > tag is as follows:

< SCRIPT runat=server Language=jscript >
Procedure definition
</script >

The Runat=server property here notifies the WEB server to process the script on the server. If this property is not set, the script is processed by the client browser. The LANGUAGE property determines the scripting language used for this script block. You can specify any language that has a script engine. Use VBScript to specify VBScript, and to specify JScript with JScript. If you do not set the LANGUAGE property, the script block is interpreted in the main scripting language.

In VBScript, processes are divided into two categories: Sub processes and Function processes. A Sub procedure is a set of VBScript statements that are contained between a sub and an End Sub statement, performing an action but not returning a value. Sub procedures can use parameters (constants, variables, or expressions passed by the calling procedure). If the Sub procedure has no arguments, the sub statement must contain an empty bracket ().

A Function procedure is a set of VBScript statements that are contained between a function and an End Function statement. A Function procedure is similar to a Sub procedure, but a Function procedure can return a value. A Function procedure can use parameters (constants, variables, or expressions passed by the calling procedure). If the Function procedure has no arguments, the function statement must contain an empty bracket (). A Function procedure returns a value that is assigned to a function name in the statement of a procedure by its name. The data type of a Function return value is always a Variant. In the following example, the Sub procedure uses two intrinsic (or built-in) VBScript functions, namely MsgBox and InputBox, to prompt the user for information. It then displays the results calculated based on this information. The calculation is done by the Function procedure created using VBScript, and the Celsius function converts Fahrenheit to degree Celsius. Sub procedure ConvertTemp When this function is called, the variable containing the parameter value is passed to the function. The conversion result is returned to the calling procedure and displayed in a message box.

Sub converttemp ()
temp = InputBox ("Please enter Fahrenheit temperature.") ", 1)
MsgBox "Temperature" & Celsius (temp) & Celsius. "
End Sub
Function Celsius (fdegrees)
Celsius = (fDegrees-32) * 5/9
End Function

The way to pass data to a process is to use parameters. The parameter is used as a placeholder for the data to be passed to the procedure. The parameter name can be any valid variable name. When you create a procedure by using a SUB statement or Function statement, the procedure name must be followed by parentheses. The parentheses contain all the parameters, 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 get data from a procedure, you must use a Function procedure. Keep in mind that a Function procedure can return a value, and a Sub procedure does not return a value.

The above gives you a brief account of VBScript, because of the length of the reasons, the author can not be a detailed description of all the knowledge of VBScript. However, because the ASP itself is not a programming language, so in the process of writing ASP applications, you must use scripting language to achieve many special features, so a flexible mastery of scripting language is essential for a programmer who writes WEB applications using ASP. Whether you are a master of rich programming experience or a beginner, as long as you now want to write WEB applications through ASP, the authors strongly recommend that you master at least one scripting language (such as VBScript). At the end of this article, the author will leave a class assignment for everyone, I hope you can learn about books by self-study and in practice to quickly master the scripting language.
Homework After class:

The author is using ASP to make a web-based BBS system, the author hopes to add a special function, that is, when any user log on to the BBS can be consulted nearly seven days of all new information published. Since the ASP's own built-in objects and components do not provide this functionality, the author invites you to start with VBScript to design such a feature. If you have any questions please EMAIL the author, the answer will be published in the next post. Please pay attention.


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.