ASP tutorial: ASP Script Looping statements

Source: Internet
Author: User
Tags html form input

From the beginning of this article, the author from the ASP built objects, for everyone in detail analysis of ASP six built-in objects and various components of the characteristics and methods.

Before you start learning about ASP's built-in objects and components, let's begin by understanding some basic concepts that will help you in your future studies. Take a look at the following table:

Active Server A collection of server-side technologies delivered with Windows NT. These technologies provide consistent server-side components, script models, and an integrated set of system services for component application management, database access, transactions, and messaging.
Active Server Pages (ASP) A server-side scripting environment that runs ActiveX scripts and ActiveX components on the server. Developers can combine scripts and components to create WEB-based applications.
Activex Rich Microsoft technical terminology that allows developers to create interactive components for the world Wide Web. A set of non-language-dependent interoperability technologies that allow software components written in different languages to work together in a networked environment. The key elements of ActiveX are the Component Object Model (COM) and the Distributed Component Object Model (DCOM). These technologies have been licensed by an open organization and have been ported to many platforms.
Ado The Active data object. A set of object-based data access interfaces optimized for data-centric applications based on the Internet. ADO is based on published specifications and comes with Microsoft Internet information Server and Microsoft Visual InterDev.
Dsn The data source name. Open Database Interface (ODBC) is used to refer to the logical name of the driver or other information required to access the data. The name of the Internet Information Server used to connect to an ODBC data source, such as a SQL Server database.
Event: Events Any action that is generated by the user or by the ActiveX control that responds to the program. General events include pressing the keyboard key, clicking the Select button by using the mouse, and other mouse actions. Programmers write code that responds to these actions.
Object: Objects In object-oriented programming, a variable consisting of operations and data that is treated as a complete entity. Objects are based on a specific model, in which the client uses the object's service to access the data of the object through an interface of a set of methods or related functions. The client can then invoke these methods to perform a certain operation.
Odbc Open Database interface. An application programming interface that allows applications to access data from existing data source standard specifications that are accessed from a variety of cross-platform data.
Sql Structured Query Language Structured query Language. Define and access international standards for relational databases.


What is an object? It doesn't mean you're in love with a partner or a date. In object-oriented programming, an object is a variable that consists of operations and data as a complete entity. Objects are based on a specific model, in which the client uses the object's service to access the data of the object by means of an interface of a set of methods or related functions, and then the clients can invoke these methods to perform some action. An ActiveX component is the key to building a WEB application, and the component provides the object that performs the task in the script. An ActiveX component is a file that contains code that performs a task or set of tasks, because the component can perform common tasks so that programmers do not have to create the code themselves to perform these tasks. You can take advantage of components as scripts and basic building blocks based on Web applications. As long as you know how to access the objects provided by the component, even beginners of bit scripting can write scripts without knowing how the components work. In summary, components enable you to write powerful scripts without having to learn to program. A component is an executable code that is contained in a dynamic-link library. dll or an executable file. exe. A component can provide one or more objects and the methods and properties of an object. To use the object provided by the component, create an instance of the object and assign the new instance the variable name. An instance of an object can be created using the ASP's Server.CreateObject method, which can be named for an object instance using a variable assignment instruction in the scripting language. The following example:

Set db=server.createobject ("ADODB. Connection ")

The variable db here is an object instance created by the ASP program to access the database.

Active Server Pages provides built-in objects that you can use in your scripts. These objects make it easier for users to gather information sent through a browser request, respond to a browser, and store user information, freeing the object developer from a lot of tedious work. The current ASP version provides a total of six built-in objects, which let's learn from the examples separately.

I. Request Object

You can use the Request object to access any information that is delivered based on an HTTP request, including parameters, cookies, and user authentication passed from an HTML table using the POST method or the GET method. The Request object enables you to access the binary data that the client sends to the server.

Syntax for Request:

Request[. Collection | properties | method] (variable)

Here the author will pick up some common object syntax for analysis

1. Form

The form collection retrieves the values of the table elements that are sent to the HTTP request body by using the POST method's table.

Grammar

Request.Form (Element) [(index) |. Count]

Parameters

element specifies the name of the table element to retrieve for the collection.

The index optional parameter that allows you to access one of several values in a parameter. It can be 1 to request.form (parameter). Any integer between Count.

Number of elements in the Count collection
The Form collection is indexed by the name of the parameter in the request body. The value of Request.Form (element) is an array of all element values in the request body. by calling Request.Form (element). Count to determine the number of values in the parameter. If the parameter is not associated with more than one value, the count is 1. If the parameter is not found, the count is 0. To refer to a single value in a TABLE element with multiple values, you must specify an index value. The index parameter can be from 1 to Request.Form (element). Any number in the Count. If you refer to one of several table parameters without specifying an index value, the returned data will be a comma-delimited string.

You can use a restatement character to display all the data values in a table request. For example, the user fills out a table by specifying several values, see.


For the hobby parameter, you can use the following script to retrieve these values.
html>
head>< title></title>p> please fill in your hobby,/p>
Form method= "POST" action= "form.asp" >
"P>< input type=" text "name=" hobby "size=" >< br> "
Input type= "checkbox" name= "hobby" value= "soccer" > soccer "input type=" checkbox "name=" Hobby "value=" Table tennis "> table tennis/p>
p>< input type= "submit" value= "send" Name= "B1" >< input type= "reset" value= "refill" name= "B2" ></p>
/form>
Request.Form ("hobby") Response.Write I & "br>" Next%>
/body>

Clip the above code into your Notepad (notice that the spaces after "" "are removed), save as a form.asp file and run, and the request object can display the elements individually depending on what you fill in the form or select the element content.

Of course use for ... The next loop can also generate the same output as follows:

%
For i = 1 to Request.Form ("hobby"). count<
Response.Write Request.Form ("hobby") (i) & "br>" next<
%>


2, QueryString
The QueryString collection retrieves the value of the variable in the HTTP query string, which is specified by the value after the question mark (?). Such as:

A href= "Example.asp?string=this is a sample" >string sample</a>

Generates a variable name string with a value of "This is a sample". You can also generate a query string by sending a table or by typing a query in its browser's address box.

To decrement the counter variable, you can set Step to a negative value. The end value of the counter variable must be less than the starting value at this time. In the following example, the counter variable MyNum minus 2 at a time. At the end of the loop, the total value is the sum of 16, 14, 12, 10, 8, 6, 4, and 2. Sub Newtotal ()

Dim MyNum, Total
For myNum = 2 Step-2
Total = Total + MyNum
Next
MsgBox "Sum is" & Total & ". "
End Sub

The exit For statement is used to exit the for ... before the counter reaches its terminating value. Next statement. Because it is usually only in certain special cases (such as when an error occurs) to exit the loop, you can do so in the If ... Then ... The Exit for statement is used in the TRUE statement block of the Else statement. If the condition is False, the loop will run as usual.

Finally, let's look at the for each ... Next statement, for Each ... Next Loop with for ... Next loop is similar. For Each ... Next is not to run the statement for a specified number of times, but to repeat a set of statements for each element in the array or for each item in the collection of objects. This is useful when you do not know the number of elements in the collection. Its syntax is as follows: For each element in group

[Statements]
[Exit for]
[Statements] Next [element]

If there is at least one element in the group, it goes into the for each block execution. Once in the loop, all the statements in the loop are executed first for the first element in the group. As long as there are other elements in the group, the statements in the loop are executed on each element. Exits the loop when there are no more elements in the group, and then resumes execution from the statement following the Next statement.

At this point, we have completed all the basic knowledge of scripting language VBScript, but only by reading the existing articles you are unable to skillfully use VBScript, you have to improve their level through continuous practice. Of course, if you're familiar with C, you can also choose JavaScript as the scripting language for your ASP application. I do not know if you have found that the ASP program debugging is difficult, because there is no ready-made tools, here I give you a brief introduction of Microsoft Script Debugger, we can use it for a certain amount of program debugging work.

IIS4.0 includes the Microsoft script Debugger (Script Debugger), which provides debugging capabilities for the Scripting program. You can use the Microsoft Script Debugger to work with scripts written in VBScript, JScript, and debugging Java applets, beans, and ActiveX components.

Some scripts are executed on the client side of the browser, and some scripts (<%.%>) are executed on the server side. Microsoft script Debugger, which can debug the script executed by the client and the server-side execution of the script program. The script executed in the client browser is executed in the client's browser, including VBScript, the Jscript section in standard HTML code. This includes the script's HTML code when the browser loads this HTML code or, for example, presses a button to trigger an event. A script that is executed by the client browser, primarily for functions such as basic checking of HTML form input.

Scripts executed on the server side are executed on the IIS server side, including in the. asp program. Executes on the IIS server first, and the execution results produce standard HTML code, which is then transferred to the client browser. Server-Side execution of the script, mainly for the link between multiple pages, HTML form input processing, and access to the database on the server data, and so on.

Microsoft Script Debugger provides the following debugging features:

1, set the break point

2. Follow up the script procedure gradually.

3, set bookmarks.

4, view the call stack.

5. View and change values.

6. Execute script instructions.

From the next section, we'll start learning about ASP's built-in objects, so stay tuned.

ASP tutorial: ASP Script Looping statements

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.