ASP Tutorial: Initial Contact Learning ASP Script Program

Source: Internet
Author: User
Tags execution iis mail split
Programs | scripts | tutorials

Why do you want to learn scripting language? What's the relationship between ASP and scripting languages?

First of all, let's say ASP is what you are. ASP, the abbreviation for Microsoft Active Server Pages, is a server-side scripting environment that you can use to create interactive Web pages and build powerful Web applications. Can show that ASP is a server-based scripting environment, then understand why the ASP needs IIS support, because IIS is a common class of Web servers; That's why you learn scripting languages, because the ASP provides a scripting environment.

Again, the ASP is just an environment, not a language. If you really want to visualize that ASP is a Web programming language, it is also a powerful hybrid language for the built-in objects and components provided by a html+ script +asp.

So, learning a little script is very important! In fact, whether it's a scripting language or any other language, I think the first thing I need to know is the various features that the language covers, includes data types, events, methods, objects, attributes, syntax, and so of course, it's all empty talk, just as in a college class, a computer language class, the pure research is to feel some rope and tasteless. Or practical good, can solve the problem of the program is a good program (proof can catch mice cat is a good cat classic holy language, hehe)

Of course, the language of this thing still want you to study well, such as you write the absolute value of the program, in fact, an ABS () function is done. For example, you will be VB script, but the same program to replace the same Java script will be wrong, their data type is different, the syntax of the sentence is not the same. As in Chinese and foreign languages, the pronunciation of the mark (data type), how to pronounce (method), how to say a sentence (grammar) ... Oh, a little make you uncomfortable. Yes! Straight up. What you need to pay attention to in the future is the function and syntax of the language. Of course not to let you all look, follow the tutorial in the encounter a master. The tutorial finished, almost also finished. Oh, you have to believe me.

There are currently two main scripting languages: VBScript and JavaScript. Because VBScript is the default server script for IIS, what we're talking about is basically an ASP that is scripted around VBScript, and of course it doesn't rule out the use of JavaScript sometimes.

To give a very simple example:

<script language= "VBScript" >
<!--
Sub Button1_onclick
MsgBox ("Welcome to use vbscript!")
End Sub
-->
</script>
<script language= "JavaScript" >
<!--
function b213 ()
{
Alert ("Welcome to use javascript!")
}
-->
</script>
<form>
<input type= "button" Name= "Button1" value= "VBS" ><br>
<input type= "button" Name= "Button2" value= "JS" >
</form>

The sub in VBScript is an event procedure. The procedure name consists of two parts: a button name, Button1 (obtained from the Name property in the < INPUT > tag), and an event name, or OnClick. The two parts are connected by an underscore (_). When you click the button, Internet Explorer looks up and runs the appropriate event procedure, that is, Button1_onclick, and MsgBox is a function, and for its more specific parameters, you look good.

On the spur of the moment, maybe everyone can see the internet a kind of crazy disgusting dialog box group, what is the principle? Please see:

<script language= "VBScript" >
MsgBox "You know you'll be a little bit sure .... "
MsgBox "No point also points ... "
MsgBox "No way Is it ...?" "
MsgBox "Otherwise the webpage can't open ah ... "
MsgBox "This is a trick ... "
MsgBox "Really want to beat the webmaster ... "
</script>

In fact, the webmaster use these to play with others, the first play with their own: because this is what he came up with AH:
Oh, I did not damage everyone's meaning, just to learn.

In the open page and page back pop-up dialog box principle

<script language= "VBScript" >
<!--
Sub Window_onload ()
MsgBox "Welcome you!"
End Sub
Sub Window_onunload ()
MsgBox "You late!"
End Sub
-->
</script>

Open page display "Welcome you!", close the page to show "See You late! ”

The above is just a display dialog box MsgBox, and of course, the Writing dialog box InputBox also by the way.

<script language= "VBScript" >
<!--
Dim strname
Strname=inputbox ("Enter your name please:", "Enter your name", "Cnbruce")
document.write (strname& ", welcome you!<p>")
-->
</script>

About the InputBox of the parameters also go to see it.

Let's look at a few more useful functions:

Len (): Returns the length of a string or the byte length of a variable
LEF (): Intercepts the contents of the first part of a string
Right (): intercepts the latter part of a string

<script language=vbs>
text= "1234567890ABCDEFGH"
i=10
Lentext=len (text)
Lefttext=left (Text,i)
Righttext=right (Text,i)
Alert ("Text length is:" &lentext)
Alert ("Text left &i&" is: "&lefttext)
Alert ("Text right Takes &i&" is: "&righttext)
</script>

According to these few, often see the long article after the "..." situation is as follows:

<script language=vbs>
text= "123 4567 8fds90 ABCDE FGHXCFV"
i=10
If Len (text) >i Then ' if the text length is greater than the given value
Text=left (text,i) ' Extracts the string of the I-bit of the preceding paragraph
Alert (text& "...")
Else
Alert (text)
End If
</script>

There is also a more important function of replace ()

Finds in a string, substituting the specified string
Replace (Strtobesearched,strsearchfor,strreplacewith)

Strtobesearched is a string; strSearchFor is the substring being looked for; strReplaceWith is the substring to be substituted.
One of the following is a very useful text-to-speech program.

<script language=vbs>
text= "Hello, welcome"
Text=replace (text, "Hello", "Nihao")
Text=replace (Text, "Ying", "Ying")
Alert (text)
</script>

Often in sending emails, add multiple addresses, the middle of the ";" to separate, that specific how to decompose a number of e-mail addresses received over it? Using the Split function: The Split function returns an array from the string.

<script language=vbs>
Cnbruce= "Webjx@163.com;webjx@webjx.com;tslxg@hotmail.com"
MyArray = Split (WEBJX, ";")
A=myarray (0)
B=myarray (1)
C=myarray (2)
Alert (a)
Alert (b)
Alert (c)
</script>

These are more realistic and better play, play in the study is also good: But after all, we have to program, to write programs, I know that the program structure can be divided into:

1, sequential structure: is the most common, from the top down, from left to right to execute the program

2, select the structure: give you two conditions, otherwise (if ... then), otherwise (else), choose:

3, loop structure: give you a maximum play space, in not beyond the scope of activities (cycle), or automatically exit (out of the loop).
The complex procedure is nothing more than the three kinds of composite structure of the use of integrated nesting, you say it.

The sequence structure is not much to say, because program execution is basically in this direction.

To see the selection structure, of course, the selection structure can also be nested.

<body>
Hello
<script language= "VBScript" >
<!--
Dim thishour
Thishour=hour (Time)
If Thishour<=12 Then
Document.bgcolor= "Red"
Document.fgcolor= "BLACK"
document.write ("Good morning!") ")
Else
If Thishour<=18 Then
Document.bgcolor= "Blue."
Document.fgcolor= "White"
document.write ("Good afternoon!") ")
Else
Document.bgcolor= "Green"
Document.fgcolor= "Yellow"
document.write ("Good evening!") ")
End If
End If
-->
</script>
The purpose of this page is to display different colors and welcome information at different times.
</body>

The above procedure is very simple, as long as can know English will understand the procedure (I understand so:)

The present hour is submitted first: hour (time);

Then and 12 compare judge, if <=12, must be morning, otherwise it is afternoon and evening;

Otherwise inside continue condition judgment, if time again <=18 words, that certainly is afternoon;

Finally, needless to say, the blind guess also knew that it was evening:

The Document.bgcolor in the previous program is the background color of the document, Document.fgcolor is the foreground color of the document (text color), and the next program changes the background color dynamically.

<script language= "VBScript" >
<!--
Sub Setbgcolor (Bcolor)
Document.bgcolor=bcolor
End Sub
-->
</script>
<form>
<input type= "Radio" name= "Color" onclick=setbgcolor ("Red") >red<br>
<input type= "Radio" name= "Color" onclick=setbgcolor ("green") >green<br>
<input type= "Radio" name= "Color" onclick=setbgcolor ("Blue") >blue<br>
<input type= "Radio" name= "Color" onclick=setbgcolor ("yellow") >yellow<br>
<input type= "Radio" name= "Color" Onclick=setbgcolor ("Gray") >gray<br>
</form>

Nesting of conditional selection structures and then show a program that detects a form

<script language= "VBScript" >
<!--
Sub Btnsubmit_onclick
If form1.name.value<> "" Then
If form1.addr.value<> "" Then
If form1.email.value<> "" Then
If InStr (Form1.email.value, "@") <>0 and InStr (Form1.email.value, ".") <>0 Then
Form1.submit
Else
Alert "Email error!"
End If
Else
Alert "Enter your email!"
Form1.elements ("email"). focus
End If
Else
Alert "Enter your address!"
Form1.elements ("addr"). focus
End If
Else
MsgBox "Enter your name please!"
Form1.elements ("name"). Focus
End If
End Sub
-->
</script>
<body>
<form name= "Form1" Method=post action= "bug.html" >
Your name:<input type= "text" name= "name" ><br>
Your addr:<input type= "text" name= "addr" ><br>
Your email:<input type= "text" name= "email" ><br>
<input type= "button" Name= "Btnsubmit" value= "Submit" >
</form>
</body>

The program is given, but it seems more difficult to understand, and sometimes the program execution time is also more important, so you have to streamline the program code.

Is the so-called: easy to write procedures, write classic procedures difficult Ah, the above program can also be a way of thinking. Use JavaScript instead (note: Learn programming ideas, don't be too fussy about script types)

<title>abc</title>
<script language= "JavaScript" >
<!--
function Form1_onsubmit ()
{
if (document.form1.name.value== "")
{
Alert ("Please set your login name.") ")
Document.form1.name.focus ()
return False
}
else if (document.form1.addr.value== "")
{
Alert ("Please fill in your address.") ")
Document.form1.addr.focus ()
return False
}
else if (document.form1.email.value== "")
{
Alert ("Please fill in your e-mail address.") ")
Document.form1.email.focus ()
return False
}
}
-->
</script>
<body>
<form name= "Form1" >
Your name:<input type= "text" name= "name" ><br>
Your addr:<input type= "text" name= "addr" ><br>
Your email:<input type= "text" name= "email" ><br>
<input type= "Submit" name= "submit" value= "Submit" >
</form>
</body>

Finally, look at the structure of the loop: 1 to 500 is not a single write out.

<script language=vbs>
For i= 1 to 500
document.write (i& "<br>")
Next
</script>

Of course, the loop can not only use for, but also with do while...loop and so on

Anyway the program this thing is instead of us doing a lot of repetition of a single boring thing-as long as you make reasonable use of the program.

Should have some achievements, learning language, learning programming is to learn grammatical semantics, learning programming architecture ideas.

Of course, this should you have a solid foundation of the language, what is the basis? Do you know what functions? Do you know how to use a program to determine an even number (including operations)? Do you know how to perform form detection? Do you know any of the three program architectures?

Oh, the above procedures thoroughly, it is best to look at the script reference manual, and I can start ASP's on the road.

Fill

A Function procedure is similar to a Sub procedure, but a Function procedure can return a value.
A Function procedure can also use a constant, variable, or expression passed by the calling procedure as a parameter. If the Function procedure has no arguments, the function statement must contain parentheses ().
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.



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.