Category: ASP



Introduction

So far you have learned how to output text, use variables and execute conditional statements. Another very powerful feature of ASP is it’s looping funciton.

FOR and NEXT Loops

FOR/NEXT loops are used when you want to execute a piece of code a set number of times. If, for example, you want to output the world ‘Hello’ 10 times, you could either code it manually or you could use:

<%
For index = 1 to 10
Response.Write(“Hello”)
Next
%>

Basically, this code says:

For index = 1 to 10

Repeat the following code until the variable ‘index’ is equal to 10, starting at 1 and going up 1 by 1.

Next

This tells the server to return to the beginning of the loop and increment the variable.

Using The Variable

A loop isn’t much use if it just does the same thing over and over again. It really offers no benefits over a simple piece of code. The real power appears when you use the counter variable in your code. If, for example, I wanted to output the numbers 1 to 10 I could use:

<%
For index = 1 to 10
Response.Write(index)
Next
&>

STEP

Step is an extra part you can add on to the end of the For line of the code to change the way it counts. In the loop above, the code starts by setting index to 1, then when Next is reached it adds another 1 (2), the next time it adds another 1 (3) and so on. Using, STEP you can change this action. For example:

<%
For index = 1 to 10 STEP 2
Response.Write(index)
Next
%>

Would output:

246810

It is counting up in 2s. You can also count down:

For index 10 to 1 STEP -1

which will count down from 10 to 1.

While Loops

Another type of loop which can be used in ASP is the While loop. A While loop is written as:

<%
Do While thenumber<10
Resonse.Write(“Less than 10”)
thenumber = thenumber + 1
Loop
%>

To explain this code:

Do While thenumber<10

This code first checks if the variable thenumber has a value which is less than 10, then if it is executes the following code until it reaches:

Loop

This tells the code to return to the Do line. Now, you may have noticed the problem here. If all the Do line does is check whether thenumber has the value of less than 10, the loop will go on forever. This is why the line:

thenumber = thenumber + 1

has to be included. This increments the value of thenumber, so that it will eventually be more than 10, and the loop will end. Of course, you aren’t just limited to adding and subtracting as you are with a For loop. You can make any changes to the variable you like in the code.

Until Loops

A third type of loop is the Until loop. This is almost exactly the same as the While loop:

<%
Do Until thenumber=10
Response.Write(“Less than 10”)
thenumber = thenumber + 1
Loop
%>

The difference between this and a While loop is that the code will execute until the conditionin the Do line is met, unlike a While loop where it will only execute while the condition is met. As with the While loop you must increment the variable yourself.

Advertisements

Introduction

Over the past two parts I have shown you the basics of text in ASP and how to store it as variables. In this part of the tutorial I will show you how to use IF statements to make decisions in your scripts.

The Basics Of IF

If statements are used to compare two values and carry out different actions based on the results of the test. If statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, the then statement is executed. If not, the else statement is executed.

IF Strucure

The structure of an IF statement is as follows:

If something=somethingelse Then
Execute some code
Else
Execute other code
End If

Common Comparisions

The ASP IF statement construction is very much like plain text, but here is a quick example of a common use of ASP. In this example the user has entered a password which has been stored in the variable EnteredPassword. The idea of this script it to check whether the user has entered the correct password:

<%@ Language=VBScript %>
<%
If EnteredPassword=”password1″ Then
Response.Write(“Well done. You got the password right.”)
Else
Response.Write(“Sorry. That was the wrong password.”)
End If
%>

If the user enters the correct password (password1) the text:

Well done. You got password right.

but if you get it incorrect you will be shown the text:

Sorry. That was the wrong password.

Other IF Options

There are many of different comparisions you can make with ASP, for example you can comapre two variables:

If EnteredPassword=RealPassword Then

or different types of comparison:

If Age>13 Then

which will check to see if the age entered by the user is greater than 13.

You can also place HTML etc. in IF statements, as the ASP will continue executing a THEN statement until it reaches an Else or an End If, and will continue to execute Else statements until it reaches End If, for example:

<%
If EnteredPassword=”password1″ Then
%>
<font face=”Arial” size=”3″>Congratulations. You may enter.</font>
<%
Else
%>
<font face=”Arial” size=”5″ color=”Red”>ERROR! You cannot enter.</font>
<%
End If
%>


Introduction

In the last part I explained a little about how to write ASP and how to tell the server that you have ASP code in your file and what language it is written in. In this part I will explain what is probably the most important use of ASP: output.

Sending Output To The Browser

It’s always been a tradition of programming tutorials to begin by writing the simple ‘Hello World’ program, so this one won’t make an exception! Sending output is done using the ASP command:

Response.Write()

so to write ‘Hello World’ to the user’s browser the complete code would be:

<%@ Language=VBScript %>
<%
Response.Write(“Hello World”)
%>

Again, this code begins by telling the system that you are writing in VBScript. Then comes the Response.Write command. Basically this is made up of two parts. ‘Response’ tells the server that you want to send information to the user. There are other types of command including: Request (which gets information from the user), Session (for user session details), Server (for controlling the server) and Application (for commands relating to the application). More about these later.

The second part, ‘Write’, tells the server that the type of response you would like to send is to write information to the user’s browser. This doesn’t just have to be text, but can include variables, which will be discussed in more depth later in this tutorial.

Variables

Probably the most important feature of a programming language is a variable. A variable is basically a way of storing text, numbers or other data, so that it can be referenced later. For example, to change the earlier ‘Hello World’ script:

<%@ Language=VBScript %>
<%
OutputText = “Hello World”
Response.Write(OutputText)
%>

The output of this code will be exactly the same as the first script, but it is fundementally different as it uses variables. Basically what this code does follows:

OutputText = “Hello World”

This line sets up a variable called OutputText and stores in it the string of letters ‘Hello World’. As this is now stored in a variable, you can now reference this text you have stored in any part of your script, and you can also manipulate it. The next line:

Response.Write(OutputText)

tells the server that you are sending information to the browser, and that the information to be sent is the contents of the variable called OutputText. Please note that the variable name is not enclosed in quotation marks. If you did this the browser would simply output the title of the variable as text.

There is a second way of outputting the values of variables, other than using Response.Write. The earlier code could have been written:

<%@ Language=VBScript %>
<%
OutputText = “Hello World”
=OutputText
%>

In this example, the = sign is used instead of ResponseWrite.

Variable Operations

The main benefits to storing information in variables is that you can use the text over and over again. For example, once storing “Hello World” in the variable OutputText, I can then use it in various places in my code:

<%@ Language=VBScript %>
<%
OutputText = “Hello World”
%>

This is my <% =OutputText %> script. The whole reason for it is to output the text <% =OutputText %> to the browser.

which would display in the browser:

This is my Hello World script. The whole reason for it is to output the text Hello World to the browser.

You can also do various operations on text stored in variables using len, left and right.

The len function simply tells you how many characters are in a string, so if you used the following code:

<% =len(OutputText) %>

The server would return to the browser the
length of the text stored in OutputText, in this case “Hello World”, so the browser would display the number 11 on the screen. You could also assign this value to a variable using:

<% StringLength = len(OutputText) %>

which would set the value of the variable called StringLength to 11.

You can also use the functions left and right. These will display only part of the variable. For example:

<% =left(OutputText, 2) %>

which would display:

He

and the code:

<% =right(OutputText, 4) %>

would display:

orld

Basically, these functions take the number of characters specififed from the left or right of the string, so left(“Some Text”, 5) takes the first 5 characters of the text.


Introduction

For any webmaster, once you have created a page with graphics and content, the next logical step is to make it interactive. You can, of course, go to one of the remotely hosted scripting sites who will provide you with a simple piece of code to put on your site, but there is a lot more flexibility if you can create and install your own scripts which will do exactly what you want.

It’s thought by many that this ‘server-side scripting’ (it is processed by the server and not the browser, so unlike JavaScript the use of ASP doesn’t depend on someone’s browser supporting it) is very difficult to learn, and this has come from the early languages like Perl, which are difficult to write and even more difficult to debug. Over the past few years two new languages have emerged, PHP and ASP. These are easy enough for even the novice webmaser to learn.

What Is ASP?

ASP stands for Active Server Pages. It is basically a server-side scripting language designed for the Windows Platform, although it is available on Unix/Linux systems through new systems, although PHP is the more popular choice for this platform. Active Server Pages is based around VBScript, a variant of Visual Basic, which makes it very easy to use as the majority of the commands are plain English and simple to decipher.

As mentioned earlier, ASP is a server-side scripting language. Basically what this means is that if an ASP page is requested, the web server will process it and run all the ASP code, before sending the output to the browser. This has two major advantages over client-side (processed by the browser) scripts like JavaScript. The first is that there are no compatibility problems. It doesn’t matter if the user is using the latest browser or the oldest, they will see the same output. The second is that your code is hidden. Because code is executed on the server, users only ever see the output, so it is safe to put passwords etc. in your ASP code.

What Do I Need?

ASP is a server-side language, so you will need to make sure that your web server has the correct software for running it. The most common setup for running ASP scripts is on a Windows-based server running IIS (Internet Information Server). It is possible to use Linux-based systems, though, but they must have the Chillisoft ASP package installed. Most web hosts will publish whether they support ASP, but if in doubt contact your systems administrator. If you need a free web host supporting ASP, try visiting Free-Webhosting.info.

Once you have the server ready to accept scripts, running one is as easy as simply uploading and running the file. You don’t need to put it in any particular place on the server or change any settings. Just upload and run.

ASP Code

When writing ASP you don’t need to worry about changing all your HTML, you simply add ASP code into your HTML pages where needed. YOu also don’t need any special software on your computer, a simple text editor will do. To begin an ASP page you will first need to tell it what language you have written it in. The most common (and the one used in this tutorial) is VBScript. You should begin your page with:

<%@ Language=VBScript %>

All this code does is tell the ASP system that you are writing your page in VBScript. You will notice that the ASP code is enclosed in special tags. All ASP code should be enclosed in the ‘percent sign tags’ in the form:

<% ASP Code Here %>

Code can be written over multiple lines, but any code not enclosed in the ASP tags will simply be treated as HTML. Similarly and HTML inside these tags but not specifically sent as output by the code will cause an error.

Testing ASP

Before you start writing scripts it is a good idea to test whether ASP will run correctly on your server. Make a simple page with the following:

<html>

n<head><title>Test Page</title></head>
<body>
This is some HTML. Below this I have ASP<br>
<%@ Language=VBScript %><br>
Nothing should appear above here.
</body>
</html>

and save it as test.asp. Then upload this to your server and access it with your browser. If it has worked correctly, the page should display and you should only see the lines:

This is some HTML. Below this I have ASP

Nothing should appear above here.

If the ASP appears in the page or the source of the page, something has gone wrong. Check the code and also the settings on your server. No ASP should appear as it should have been processed by the server before it was sent to the browser.


If you are a small or medium business looking for a website design or website redesign for your business, you have found the right place. Our professional business concept will convince you that you can afford a well designed professional website for your business. We are serving mostly in the North America.

Free Business Consultation
Our web design and SEO consultations are FREE. We can happily meet with you over the  phone or by email to discuss your business website design needs. We Believes no question is a silly question – all of us know  nothing about something.

Benefits with I Web Guy’s Services

  • Conceptual designs based on your business
  • In-House Development
  • On time Project Delivery
  • Cost-Effective Products Services & much more