ASP Basics Tutorial: ASP script variables, functions, processes, and conditional statements

Posted by admin 0 comments

In the period of the last of you a brief introduction to ASP VBScript scripting language of some basic common sense, this issue will continue to address you on the VBScript scripting methods, and VBScript in the ASP program by showing the process of preparing the series of examples of Shi Members VBScript has one step further on understanding.

In the study of the scripting languages VBScript variables, constants, and the basic concepts of the process, the current will continue to introduce the functions and syntax of VBScript.

The same functions and procedures are named blocks of code, but there is a huge difference between them, the process to complete the process task, the function returns value. We can understand this, the process as a complete sentence, while the function is like a word. For example, if you want to get a certain number of square root, you just pass the number of VBScript for Sqr () function, this function will immediately return to the square root of the number. Such as:

A = sqr (9)
 
Then A = 3. Proficiency in scripting language function ASP program will prepare you bring great convenience to the end of more than one layout for all after-school practice, if you master the scripting language of function not comprehensive enough, then the solution so The problem will be very small, it can take considerable effort. Now let’s look at the Road after-school practice.

“On the use of ASP is the production of a WEB-based BBS system, hoping to add a special feature in which, that when any user login to the BBS are able to access the past seven days later to all the newly released information.”

If you are not familiar with VBScrip, will not know VBScrip itself to get a date, or the difference between and the function DateSerial, its syntax is as follows:

DateSerial (year, month, day)
 
If you want to specify a date, for example: November 10, 1998, then the DateSerial function in the range of each parameter should be acceptable, today’s value should be between 1 and 31, on the value should be between 1 and 12. However, you can use that before or after a certain day, month and year on the number of values for each parameter to specify the relative expression dates. The following sample uses the numeric expressions instead of absolute dates. Here, DateSerial function to return on Nov. 10, 1998 before the second decade (1990-20) zero-two months (11-2) then day (10-1) Date: namely, September 9, 1978. Procedures are as follows:

Datep = DateSerial (1998-20, 11-2,10-1)
 
For the year argument, if the range is from 0 to 99, 1900 was interpreted in 1999. For this year outside parameters, then use the four-digit year (eg 1800). When the value of any parameter within the acceptable range, will be due to rounding to the next larger unit of time. For example, if you specify 35 days, then this will be interpreted as the number of days a month plus the extra number of days, the extra number of days depends on the year and month. However, if the parameter values beyond the range of -32,768 to 32,767, or specified by the three parameters (either directly or through an expression specified) date beyond the acceptable date range, an error occurs.

When we understand and grasp the DateSerial function to use, the layout of the problem asks another look on purpose, everything solved. Now I will process the code published in this section are as follows:
itemp = DateSerial (Year (date), month (date), day (date) -7)

itemp = DateValue (itemp)
sql = “Select * from message Where message.creatime Between #” & date & “# And #” & itemp & “#”
 
Here we come into contact with a set of functions Year, month, day, which is used to get a date, month, day. date is a constant, said today’s date, the function DateValue sucked into a date format string variable variable. Procedures in this paragraph the third line, we first exposed to a standard SQL query statements, sentence statement, what does that mean?

“Select” is a standard SQL database query command, through the SELECT statement can retrieve data in the database, and query results to the user, where the “*” indicates that the query of the name “message” to all records in the database and “where” the role is to set a query, the database in order to meet the conditions of the records taken out, “message.creatime” is a record stored in the database to create date variables. Statement to link the sentence to understand is: query the database, called message to all records, including creation date and today and today all the records within seven days previously stored in the variable sql. We first contact may be due to SQL statement, a time not fully understand its role, but do not worry of the next chapter will be devoted to you with an introduction to use SQL.

Through the above study, we should have been able to understand the role of function in the program and, of course we do not have to memorize function, but use only a shortcut to achieve proficiency – and more practice. Next, let’s look at the basic syntax of VBScript.

Understand the programming language’s friends must know the procedures to control program flow statements can be divided into conditional statements and loop statements, VBScript can be used in the following conditional statement: If … Then … Else statement

Select Case statement

If … Then … Else statement is used to calculate whether the conditions for the True or False, and specify the basis of calculation of statements to run. Typically, the condition is the use of comparison operators on the value or variable to compare the expression, If … Then … Else statements can be nested as needed.

Let’s create two sample files: if1.asp and if2.asp

The following statement in the clip to the notebook and save it as if1.asp (Note: Please process “<” remove the space after) <html>

<Head>
<TITLE> if1.asp </ TITLE>
</ Head> <body bgcolor = “# FFFFFF”>
<Form action = “if2.asp” method = get>
Your First Name <INPUT NAME = “FirstName” MaxLength = 20> <p>
Your Last Name <INPUT NAME = “LastName” MaxLength = 20> <p>
<INPUT TYPE = submit> <INPUT TYPE = reset>
</ Form>
</ Body>
</ Html>

The following statement in the clip to the notebook and save it as if2.asp

<Html>
<Head>
<TITLE> ifrespond.asp </ TITLE>
</ Head>
<% Fname = request.querystring (“Firstname”)
lname = request.querystring (“Lastname”)
If fname = “George” and lname = “Washington” then%>
Hi.You must be the first president!
<% Else%>
Hi! Nice to Meet You
<% End if%>
</ Body>
</ Html>

asp1.asp generate a text input box, requires users to enter name, name

asp2.asp IF statement is used to judge whether the user input the name “George Washington”, and make corresponding feedback. The problem we built an ASP object request, by using the request object can access any HTTP request with the message, including from the HTML form using POST method or GET method of passing parameters, cookie and user authentication. The QueryString collection retrieves HTTP query string variables, HTTP query string by a question mark (?) After the value specified. Such as:

http://localhost/if2.asp?Firstname=George&Lastname=Washington

Generate a value of “Firstname = George & Lastname = Washington” string variable name. About ASP object of the next few years will focus on.

If … Then … Else statement is a distortion that allows you to select from several conditions, which add ElseIf clause to expand the If … Then … Else statement function that allows you to control based on a variety of possible program flow.

Part of our expansion program will asp2.asp as follows: <%

fname = lcase (request.querystring (“Firstname”))
lname = lcase (request.querystring (“Lastname”))
If fname = “george” and lname = “washington” then%>
Hi.You must be the first president! <P>
<% ElseIf fname = “ronald” and lname = “reagan” then%>
Hi.You must be the actor president! <P>
<% ElseIf fname = “jimmy” and lname = “carter” then%>
Hi.You must be the peanut farmer president! <P>
<% ElseIf fname = “naoko” or fname = “charles” then%>
Hi.Your name reminds me of someone, but I am not sure who! <P>
<% Else%>
Hi! Nice to Meet You
<% End if%>

You can add any number of ElseIf clauses to provide a variety of options. But the use of multiple ElseIf clauses often make procedures become cumbersome. In a number of conditions to choose the better approach is to use Select Case statement.

Select Case structure provides the If … Then … ElseIf structure of an alternative form, you can choose from more than one block in the implementation of one of them. Select Case statement to the function and If … Then … Else statement similar to, but you can make the code more concise and readable. The Select Case structure in the beginning count only once using a simple test expression. The results of the expression and structure of the value of each Case Comparison. If they match, the implementation of the Case associated with the block, we can also use the Select Case statement to write asp2.asp file:

<%
fname = lcase (request.querystring (“Firstname”))
lname = lcase (request.querystring (“Lastname”))
name = fname + lname
Select case name
case “georgewashington”
response.write “Hi.You must be the first president! <p>”
case “ronaldreagan”
response.write “Hi.You must be the actor president! <p>”
case “jimmycarter”
response.write “Hi.You must be the peanut farmer president! <p>”
case “naokocharles”
response.write “Hi.Your name reminds me of someone, but I am not sure who! <p>”
case else
response.write “Hi! Nice to Meet You”
End Select%>
 
Note that the Select Case structure, only the calculation at the beginning of an expression, and only once, and If … Then … ElseIf structure calculation expression for each ElseIf statement, these expressions can vary. Therefore, each ElseIf statement only if the expression is the same calculation, we can use the Select Case structure instead of If … Then … ElseIf structure. Select Case statement also can be nested, each layer of nested Select Case statement must have matched the End Select statement.

More to you about the scripting languages VBScript functions and conditions of use statement, because of limited space can not be more reason to start and hope that friends who wish to learn ASP, can a degree of self-study after school and practice.

Filed in Asp 0 comments
No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
«
Next Post
»