Advanced ASP programming (3): Accessing script programs
With the enhanced knowledge of form in the previous HTML, is it possible to start the ASP journey? Not necessarily.
Programmers who have systematically studied ASP will understand that it is always crucial to learn the scripting language before the system learns the embedded objects and components of ASP.
Why? Why should we learn the scripting language? What is the relationship between ASP and scripting?
First, let's talk about why ASP is expensive. ASP is short for Microsoft Active Server Pages. It is a server-side scripting environment that can be used to create interactive web pages and build powerful web applications. It can be indicated that ASP is a server-based Script environment, so you can understand why ASP requires IIS support, because IIS is a common web server; then you can understand why you want to learn the scripting language, because ASP provides a script environment.
Once again, it should be noted that ASP is only an environment, not a language. To visualize ASP as a network programming language, it is also a hybrid language with powerful functions of built-in objects and components provided by HTML + script + ASP.
Therefore, learning scripts is very important! In fact, whether it is a scripting language or other languages, I think the first thing we need to know is the various features covered by the language, including: of course, data types, events, methods, objects, attributes, and syntaxes are all empty talk. Just like a computer language course in a university class, the pure research work is a bit boring. The program that can solve the problem is a good program (the cat that can catch a mouse is a classic swordsmanship of a good cat, haha ).
Of course, you still need to study the language. For example, if you write a program that calculates the absolute value, an ABS () function is actually done. For example, if you use a VB script but replace the same program with a Java Script, an error will occur. Their data types are different and the statement syntax is different. Just like Chinese and foreign languages, we need to publish a phonetic alphabet (data type), how to pronounce it (method), and how to say it as a sentence (syntax )...... well, it makes you feel a little uncomfortable. OK! Directly. You need to pay attention to the functions and syntax of the language in the future. Of course, it is not for you to take a full look. Follow the instructions in the tutorial to master one. After completing the tutorial, it's almost over. You have to trust me.
Currently, there are two main scripting languages: VBScript and JavaScript. Because VBScript is the default server script for IIS, we have basically learned about ASP, which uses VBScript as the script. Of course, JavaScript is not ruled out in some cases.
Here is a simple example:
<Script language = "VBScript">
<! --
Sub button1_onclick
Msgbox ("Welcome to VBScript! ")
End sub
-->
</SCRIPT><Script language = "JavaScript">
<! --
Function b213 ()
{
Alert ("Welcome to vodavasert! ")
}
-->
</SCRIPT>
<Form>
<Input type = "button" name = "button1" value = "vbs"> <br>
<Input type = "button" name = "button2" value = "JS" onclick = "b213 ()">
</Form>
Sub in VBScript is an event process. The process name consists of two parts: one part is the button name, that is, button1 (from the name in the <input> tag
The other part is the event name, that is, onclick. The two parts are connected by underscores. When you click the button, Internet Explorer looks for and runs
The corresponding event process, that is, button1_onclick, and msgbox, is a function. For more specific parameters, you can see it.
For a moment, everyone may see a crazy dialog box group on the Internet. What is the principle? See:
<Script language = "VBScript">
Msgbox "you know you will click OK ....... "
Msgbox "does not point also points ........ "
Msgbox "no way, right ..........? "
Msgbox "or the webpage cannot be opened ...... "
Msgbox ", which damages people ......... "
Msgbox "really want to beat the webmaster ......... "
</SCRIPT>
In fact, when the webmaster used this to play with others, he first played with himself: because it was all he came up :)
Haha, I didn't mean to hurt everyone, just to learn.
How does one bring up a dialog box on the open and back pages!
<Script language = "VBScript">
<! --
Sub window_onload ()
Msgbox "welcome you! "
End sub
Sub window_onunload ()
Msgbox "see you late! "
End sub
-->
</SCRIPT>
The page displays "welcome you! "," See you late! "is displayed on the off page! "
The above is just a display dialog box msgbox, and of course there is a writing dialog box inputbox. Let's take a look.
<Script language = "VBScript">
<! --
Dim strname
Strname = inputbox ("enter your name please:", "enter your name", "cnbruce ")
Document. Write (strname & ", welcome you! <P> ")
-->
</SCRIPT>
Take a look at the parameters for inputbox.
Let's take a look at several practical functions:
Len (): returns the length of the string or the length of the variable in bytes.
Lef (): extract the first part of a string.
Right (): extract the part of the string.
TEXT = "1234567890 abcdefgh"
I = 10Lentext = Len (text)
Lefttext = left (text, I)
Righttext = right (text, I)
Alert ("the text length is:" & lentext)
Alert ("retrieve text from the left" & I & ":" & lefttext)
Alert ("right-click the text" & I & ":" & righttext)
</SCRIPT>
According to the above, we often see the situation that "......" is added after a long article 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 I-bit string from the front
Alert (text &"...")
Else
Alert (text)
End if
</SCRIPT>
There is also an important function Replace ()
Searches for strings to replace the specified strings.
Replace (strtobesearched, strsearchfor, strreplacewith)
Strtobesearched is a string; strsearchfor is a substring to be searched; strreplacewith is a substring to replace.
The following is a useful text Conversion Program.
<Script language = vbs>
TEXT = "Mom, I rely on"TEXT = Replace (text, "Mom", "MD ")
TEXT = Replace (text, "by", "kao ")
Alert (text &"...")
</SCRIPT>
These are more realistic and interesting, and it is good to study in middle school :) but after all, we want to program. When it comes to programming, I know the program structure:
1. Ordered Structure: it is the most common program that runs from top to bottom and from left to right.
2. Select the structure: Here are two conditions for you to select either (if... then) or (else :)
3. Loop Structure: it gives you the maximum space to perform activities (loops) within this range. Otherwise, the system automatically exits (jumps out of the loop ).
Complex programs are simply applied by the comprehensive nesting of these three structures.
There is not much to say about the sequence structure, because the execution of the program is basically in this direction.
Let's take a look at 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>
This page displays different colors and welcome information based on time.
</Body>
The above program is very simple, as long as you can understand English, you will understand the program (I understand it so :)
First, submit the current hour: hour (time );
Then compare it with 12. If <= 12, it must be in the morning; otherwise, it is in the afternoon and evening;
Otherwise, the condition is further determined. If the time is less than or equal to 18, it must be afternoon;
Finally, needless to say, the blind man guessed it was night too :)
Document. bgcolor in the previous program is the background color of the document, document. fgcolor is the foreground color (text color) of the document, and the next program dynamically changes the background color.
<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 the conditional selection structure and then show a form detection program.
<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 provided, but it seems difficult to understand. Sometimes the execution time of the program is also important, so you have to streamline the program code.
The so-called: easy to write programs, difficult to write classical programs, then the above programs can also change the way of thinking. Use javascript instead ).
<HTML> <Script language = "JavaScript">
<! --
Function form1_onsubmit ()
{
If (document. form1.name. value = "")
{
Alert ("set your login name. ")
Document. form1.name. Focus ()
Return false
}
Else if (document. form1.addr. value = "")
{
Alert ("Enter your address. ")
Document. form1.addr. Focus ()
Return false
}
Else if (document. form1.email. value = "")
{
Alert ("Enter your email address. ")
Document. form1.email. Focus ()
Return false
}
}
-->
</SCRIPT>
</Head>
<Body>
<Form name = "form1" onsubmit = "Return form1_onsubmit ()">
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>
</Html>
Finally, let's look at the next loop structure: 1 to 500 is not written one by one.
<script language=vbs>
for i= 1 to 500
document.write(i&"<br>")
next
</script>
Of course, loop can not only use for, but also do while... loop.
In any case, the program replaces us with a lot of repetitive and boring things-as long as you make rational use of the program.
There should be some achievements. Learning Language and programming means learning syntax and semantics and learning programming architecture ideas.
Of course, this requires you to have a solid foundation of the language. What is the basis? Do you know which functions? Do you know how to use the program to determine the even number (including the operation )? Do you know how to perform form detection? Do you know the three program architectures?
Function reference: http://www.cnbruce.com/blog/showlog.asp? Cat_id = 26 & log_id = 245
Haha, I have thoroughly understood the above programs. I 'd better look at the script reference manual and start ASP with me.