Learnem, Computer education over Internet - Since the year 2000
Home Ebook E-learning Courses Free E-books Login Discussion Boards
ASP - Lesson 1 : Start with ASP Web programming

Start with ASP Web programming

ASP is a new scripting technology that comes with Microsoft IIS web server and enables web programmers to create powerful web applications and pages. These dynamic pages are the result of running a code on the server. Server receives user’s input from browser, runs the script and creates a web page on the fly and sends it back to browser. We can use different languages such as vbscript and jscript with asp scripting method. ASP is now available on Unix platforms by a third party company "chili soft". For Unix people asp is very similar to PHP scripting.

By learning ASP we will be able to write different applications such as search engines, user databases, chat  scripts, message boards, web mail and any dynamic web application that you see on the web.

Your first ASP script

ASP code is very similar to html code. Difference is that some part of code contains scripting commands. 

As we told latter, script languages that we can use in ASP code can be vbscript, jscript, perlscript or any other scripting language. Vbscript and Jscript are supported by default in IIS (Microsoft’s Internet Information Server that includes a web server). Other scripting languages must be installed on server before you can use them.

Let’s look at our first example:

Example 1-1: "1-1.asp"

<html>
<%test="Hello"%>
<%=test%>World!
</html>

As the resulting web page will be a standard html file, we have inserted <html> opening and closing tags in our document.

Anything that comes between <%…%> tags is assumed to be script will be run by scripting engine of IIS web server. 

Here we have used the following command between <%...%> tags:

test="hello"

This command will create a variable and assign it the value "hello".

Whenever you want to print value of a variable or expression you can use <%=…%> tags with variable name 
or expression. 

Here we have used the following:

<%=test%> world!

IIS will replace <%=test%> with actual value of variable ‘test’. Therefore result of this line of code will be:

Hello world!


If you want to run this code you must create a file with .asp extension in a directory in your web servers root. For example if your web server root is at 

"c:\Inetpub\wwwroot"

then make a directory called "aspc" in "wwwroot" directory and place "1-1.asp" file inside it. 

After this step you must be able to run it from a browser. Just point your browser to "http://localhost/aspc/1-1.asp".

If you see the words "Hello world!" then you have succeeded. If your browser tries to download asp file or an error occurs then you must find the problem. Check file contents and filename again. You must also have "script run" access for "aspc" directory. 

If browser shows an error, you may be able to find some comments at the end of error page. These errors are  issued by web server and in most cases help you to find scripting errors.

After you have succeeded running your first asp script, we are ready to go to next step: "Learning ASP programming!"

A little more …

If you have run the code you must have seen the results on your web browser. If you want to see html source of browsed page, you can right click on page and choose "view source". In this way you will be able to see what html code is actually sent to your browser.

For our previous example you must see this code:

<html>
Hello world !
</html>

As you see your browser have received a standard html page. As an asp code produces a new result each time it is being run (most of them) we call this kind of web pages as "Dynamic web pages".

A loop example

Now we will add a simple loop to our script.

If you have worked with any kind of Basic programming language, you are familiar with "for" loops.

Example 1-2: 1-2.asp

<html>
<%test="Hello"%>
<%for i=1 to 100%>
<%=test%> World!
<%next%>
</html>

As you see we have a "for" loop that will run 100 times. Therefore everything between "for" and "next" commands will run for 100 times. Result is a web page with 100 Hello worlds on it!

Again you can see the source of web page to see what has been created by script. In fact browser is not aware that it is receiving results of dynamic page that is created by a script.

It looks at it exactly like an ordinary html file.

Using Jscript instead of vbscript


As we told latter we can use other scripting languages instead of vbscript. Default scripting language of IIS is 
vbscript. If you want to override this you must specify scripting language explicitly.

We have translated our previous example to jscript language. But pay attention that we will use vbscript in our ASP course.


Example 1-3: 1-3.asp

<%@ language=Jscript %>
<html>
<body>
<%test="hello"%>
<% for(i=1;i<=100;i++){ %>
<%=test%> World ! 
<% } %>
</body>
</html>

Before closing this section let me warn you that you can not use <%=… %> and <%… %> tags inside each other. <%… %> starts a block of asp code in which you can not use <%=… %> which is intended to be used in html area of asp code.

<script>…</script> server side tags

Another method for including ASP code (to form an asp file) in html code is using <SCRIPT>…<SCRIPT> tags. We will use two parameters for <SCRIPT> tag. "Language" parameter specifies which scripting language we want to use. "RUNAT" parameter specifies that script will be run on server instead of client. Look at this example to  understand this better:

Example 1-4: 1-4.asp

<html>
<head>
<script language="vbscript" runat="server">
response.write ("This one is script output")
</script>
</head>
<body>
This is html code
</body>

Yet there is a problem.

If you run above code and look at returned source code you will see that html code is invalid. Output of script has come after </html> tag.

<html>
<head>
</head>
<body>
This is html code
</body>
This one is script output


To understand the reason for this you must know two rules in running asp on IIS

1- Anything between <script>…</script> tags will be run immediately after loading the file. So these codes 
will be run before other sections.

2- In IIS 5.0 results of script output will be buffered by default and will be sent to browser just after everything else has been sent.

In fact for above reasons we only use <script>…</script> tags to include procedures and functions that will be called later in other parts of asp page.

See a sample.

Example 1-5: 1-5.asp

<html>
<head>
<script language="vbscript" runat="server">
sub outnow
response.write("<b>Hello world!</b>")
end sub
</script>
</head>
<body>
I want to say: <br>
<%outnow%>
</body>
</html>


In above code we have used another asp object "response" to send output to browser. In next lessons we will study this object and everything about it completely. We will also study sub and functions.

Again let me warn you that <script>…</script> starts a block of asp code in which you can not use <%=…%> and <%…%> tags because these tags are intended to be used in html area of asp code not inside a asp code block.

Output of this code will be:

I want to say: 
Hello world!

And if you see html code of it you will understand that everything is going as we want.

<html>
<head>
</head>
<body>
I want to say: <br>
<b>Hello world!</b>
</body>
</html>

As you see we have used a sub program inside <script>… </script> tags it will not run immediately and will wait until we call it.

We usually insert <script>…</script> code between <head>…</head> tags as we have done in our example. Then we can call the function or sub anywhere in our asp script.

We learned a little about asp scripting in this lesson. In fact we have only learned some asp scripting concepts. In next lesson we will start to learn asp and vbscript language. 

As you see you must have a good html background so I you suggest to review html coding before we start next lesson. We will have a few simple exercises in this lesson but in next lessons we will have more complicated  applications! for our exercises.

Exercises

1- Write an asp code that does the following: 

Creates two variable and assigns two numbers to them. Calculates sum of these two variables and then prints each number and their sum in separate lines in output. Program must create a valid html page.

2- Write a program that writes your name with font sizes from 1 to 6 in separate lines. Use asp "for" loops for this purpose. Loop variable will be the font size in <font size=…> … </font> tag.

 

Next Lesson


Links
Discussion Boards
Paid Ebooks
Free Ebooks
Testimonials
Faq
Contact
About us
Course Pages
Web Design
C Programming
Free Tutorials
Web Design
PHP Web Prog.
C Prog.
ASP Web Prog.
Free Ebooks
Web Design Ebook
C Prog. Ebook
Free Support
Support for e-books and free courses are offered through CourseFarm website. In order to use the CourseFarm website, You need to create a free member account on it (see main page of the website).

Web Design in 7 Days Course Page

Programming in C in 7 Days Course Page

PHP Web Programming in 7 Days Course Page

Support on CourseFarm is provided by authors of the e-books themselves. You may also contact them on the website for your possible questions.
Home | Educational E-books | Online Courses | Free Books | Discussion Boards
Testimonial | Contact us | FAQ | About us
Society50 Asian Social Network | Science and Tech Blogs | Asia Weblog | CupidB Free MatchMaker

© 2000-2010 Learnem.com, All Rights Reserved. Last Update April, 2011.