asp basic syntax rules
TRANSCRIPT
-
8/12/2019 ASP Basic Syntax Rules
1/11
ASP Basic Syntax Rules
Write Output to a Browser
An ASP file normally contains HTML tags, just like an HTML file. However, an ASP file
can also contain server scripts, surrounded y t!e delimiters "# and #$.
Server scripts are e%ecuted on t!e server,and can contain any e%pressions, statements,
procedures, or operators valid for t!e scripting language you prefer to use.
The response.write Command
T!e response.write command is used to write output to a rowser. T!e following e%ample
sends t!e te%t &Hello 'orld& to t!e rowser(
"!tml$
"ody$
"#response.write)&Hello 'orld*&+
#$
"ody$
"!tml$
T!ere is also a s!ort!and met!od for t!e response.write command. T!e following
e%ample also sends t!e te%t &Hello 'orld& to t!e rowser(
"!tml$"ody$
"#
-&Hello 'orld*&
#$"ody$
"!tml$
Using BScript in ASP
ou can use several scripting languages in ASP. However, t!e default scripting language
is /0Script(
"!tml$
-
8/12/2019 ASP Basic Syntax Rules
2/11
"ody$
"#
response.write)&Hello 'orld*&+#$
"ody$
"!tml$
T!e e%ample aove writes &Hello 'orld*& into t!e ody of t!e document.
Using !a"aScript in ASP
To set 1avaScript as t!e default scripting language for a particular page you must insert a
language specification at t!e top of t!e page(
"#2 language-&javascript$"!tml$
"ody$
"#3esponse.'rite)&Hello 'orld*&+
#$
"ody$"!tml$
#ote$1avaScript is case sensitive* ou will !ave to write your ASP code wit! uppercase
letters and lowercase letters w!en t!e language re4uires it.
Other Scripting %anguages
ASP is s!ipped wit! /0Script and 1Script )Microsoft5s implementation of 1avaScript+. 6f
you want to script in anot!er language, like P73L, 3788, or Pyt!on, you will !ave to
install script engines for t!em.
/ariale
-
8/12/2019 ASP Basic Syntax Rules
3/11
-
8/12/2019 ASP Basic Syntax Rules
4/11
i (h
ASP Procedures
6nsert t!e "#2 language-&language& #$ line aove t!e "!tml$ tag to write t!e
procedurefunction in anot!er scripting language(
&xample
"#2 language-&javascript& #$
"!tml$"!ead$
"#
function jsproc)num9,num:+;3esponse.'rite)num9,?+#$"p$
"ody$
"!tml$
'i((erences Between BScript and !a"aScript
'!en calling a /0Script or a 1avaScript procedure from an ASP file written in /0Script,you can use t!e &call& keyword followed y t!e procedure name. 6f a procedure re4uires
-
8/12/2019 ASP Basic Syntax Rules
5/11
parameters, t!e parameter list must e enclosed in parent!eses w!en using t!e &call&
keyword. 6f you omit t!e &call& keyword, t!e parameter list must not e enclosed in
parent!eses. 6f t!e procedure !as no parameters, t!e parent!eses are optional.
'!en calling a 1avaScript or a /0Script procedure from an ASP file written in
1avaScript, always use parent!eses after t!e procedure name.
ASP )orms and User *nput
T!e 3e4uest.@ueryString and 3e4uest.orm commands are used to retrieve user input
from forms.
A form wit! met!od-&get&
How to interact wit! t!e user, wit! t!e 3e4uest.@ueryString command.
A form wit! met!od-&post&
How to interact wit! t!e user, wit! t!e 3e4uest.orm command.
A form wit! radio uttons
How to interact wit! t!e user, t!roug! radio uttons, wit! t!e 3e4uest.orm command.
User *nput
T!e 3e4uest oject can e used to retrieve user information from forms.
&xample +T,% (orm
"form met!od-&get& action-&simpleform.asp&$
irst Bame( "input type-&te%t& name-&fname& $"r $Last Bame( "input type-&te%t& name-&lname& $"r $"r $
"input type-&sumit& value-&Sumit& $
"form$
Cser input can e retrieved wit! t!e 3e4uest.@ueryString or 3e4uest.orm command.
http://www.w3schools.com/asp/showasp.asp?filename=demo_reqqueryhttp://www.w3schools.com/asp/showasp.asp?filename=demo_simpleformhttp://www.w3schools.com/asp/showasp.asp?filename=demo_radiobhttp://www.w3schools.com/asp/showasp.asp?filename=demo_reqqueryhttp://www.w3schools.com/asp/showasp.asp?filename=demo_simpleformhttp://www.w3schools.com/asp/showasp.asp?filename=demo_radiob -
8/12/2019 ASP Basic Syntax Rules
6/11
Re-uest.ueryString
T!e 3e4uest.@ueryString command is used to collect values in a form wit!
met!od-&get&.
6nformation sent from a form wit! t!e D7T met!od is visile to everyone )it will edisplayed in t!e rowser5s address ar+ and !as limits on t!e amount of information to
send.
6f a user typed &0ill& and &Dates& in t!e HTML form aove, t!e C3L sent to t!e serverwould look like t!is(
!ttp(www.w>sc!ools.comsimpleform.aspEfname-0illFlname-Dates
Assume t!at &simpleform.asp& contains t!e following ASP script(
"ody$'elcome"#
response.write)re4uest.4uerystring)&fname&++
response.write)& & F re4uest.4uerystring)&lname&++#$
"ody$
T!e rowser will display t!e following in t!e ody of t!e document(
'elcome 0ill Dates
Re-uest.)orm
T!e 3e4uest.orm command is used to collect values in a form wit! met!od-&post&.
6nformation sent from a form wit! t!e PGST met!od is invisile to ot!ers and !as nolimits on t!e amount of information to send.
6f a user typed &0ill& and &Dates& in t!e HTML form aove, t!e C3L sent to t!e serverwould look like t!is(
!ttp(www.w>sc!ools.comsimpleform.asp
Assume t!at &simpleform.asp& contains t!e following ASP script(
"ody$
-
8/12/2019 ASP Basic Syntax Rules
7/11
'elcome
"#
response.write)re4uest.form)&fname&++response.write)& & F re4uest.form)&lname&++
#$
"ody$
T!e rowser will display t!e following in t!e ody of t!e document(
'elcome 0ill Dates
7%ample for ASP
Test.!tml
?our name:
test).asp
-
8/12/2019 ASP Basic Syntax Rules
8/11
7%ample :
Test:.!tml
?our name:
Test:.asp
-
8/12/2019 ASP Basic Syntax Rules
9/11
)orm alidation
Cser input s!ould e validated on t!e rowser w!enever possile )y client scripts+.
0rowser validation is faster and reduces t!e server load.
ou s!ould consider server validation if t!e user input will e inserted into a dataase. A
good way to validate a form on t!e server is to post t!e form to itself, instead of jumping
to a different page. T!e user will t!en get t!e error messages on t!e same page as t!eform. T!is makes it easier to discover t!e error.
A orm wit! 6nformation
3irst name:
Fast name:*he inormation recei+ed rom the orm abo+e was:
9esponse.rite("*he name propertyGs count is: " 9esponse.rite(9eAuest.3orm("name".Hount
9esponse.rite("
" 9esponse.rite("3irst name=" & 9eAuest.3orm("name"()
9esponse.rite("
"9esponse.rite("Fast name=" & 9eAuest.3orm("name"(,
9esponse.rite(""
'nd i%>
-
8/12/2019 ASP Basic Syntax Rules
10/11
A orm wit! 3adiouttons
Ilease select your a+orite car:
Jol+o
/aab
2M
end i%>
A orm wit! !ecko%
hich o these ruits do you preer:
-
8/12/2019 ASP Basic Syntax Rules
11/11
>Kpple
Lrane
2anana?ou like: