Web Host Directory Web Host Directory

AU

Service ChannelsWeb Host Directory

Web Host Directory

Hosting Guides

ASP Training - The Basics: Part 2

Programmers Note


(Ignore if you don't understand.) If you are a programmer then you might have spotted that the last part is actually quite inefficient. You could miss out all of the variable setting and you could also use a select case statement instead of all those nasty ifs.


select case weekday(date)
  case 1
    weekDayActual = "Sunday"
  case 2
    weekDayActual = "Monday"
  .
  .
  .
  case 7
    weekDayActual = "Saturday"
  case else
    weekDayActual = "Ooops, something's not right here!"
end select

As you can see this is a lot shorter, and I could have used it, but this is a training guide, and as an introduction setting variables and using ifs is a lot easier to explain than trying to jump straight into nested function calls and case statements.


<%= ... %>


The final active line of the introductory script uses a special case of response.write (above). When creating the HTML you very often need to include the value of ASP variables, if every time you wanted to insert ASP variables straight into HTML you had to <% response.write([variableName]) %> you would quickly get very annoyed. <%= [variableName] %> is nice little short cut to make your life a bit easier. Just insert this into the HTML where you want the ASP variables value to appear.


Warning : although you are using <%= [variableName] %> the web server actually interprets this as if it was <% response.write([variableName]) %>. If, during the output of the variable an error occurs, the error message will not refer to <%= [variableName] %> but to <% response.write([variableName]) %> instead. This can be a bit confusing if you are not prepared for it.


That is the end of this introduction to ASP Basics. The next page will introduce you to a more practical use for ASP, the processing of HTML forms.


Summary

<% ... %> The surrounders for ASP. Bracket any ASP you write (be it in VBScript, JavaScript, perlScript, anyOtherScript) inside these tags. You will quickly see if you have not done this as you ASP will appear in your HTML page rather than being executed!
<%= [aspVariableName] or "text string" %> This is a useful, quick shortcut to get ASP variables into your HTML. It means that you can display the value of a variable to the screen without having to write <% response.write([aspVariableName] or "text string") %> every time.
Warning : the <%= is actually interpreted by ASP into response.write. Why a warning? Well, if you make a mistake and get an error message the error message output will say response.write and not <%=; which can be very confusing if you are not used to it!
@LANGUAGE="VBSCRIPT" Not really required, but nice to have, if you are using VBScript in you ASP. Essential if you are going to use a language other than VBScript in you ASP. It tells the ASP what language to use when running; the default being VBVScript.
Response.write([aspVariableName] or "text string") A way of outputting information to an HTML page whilst still being inside ASP. Useful for single lines, or for debugging where you want to see the value of a particular variable temporarily, and you'll delete the line when any problems have been sorted out.
Date

Provides you with the current date (remember, this is the date on the maching running the ASP script; i.e. the webserver of your host when you get published; not the date of the machine looking at your page)

Usage : can be used in isolation to just write the current date to the HTML (<%= date %>).  More commonly used to assign the current date to a variable, <% variableName = Date %>.

Weekday([dateType]) When called this will return a number (between 1 and 7), where 1 is Sunday, 2 is Monday, ...., 7 is Saturday. This function is commonly used with an if...elseif...else...end if; or a case select statement to change the number into the text string for the day of the week (e.g. "sunday", "monday", ...., "saturday")

Usage : dayNumber = weekDay(variableContainingDate). dayNumber will be a number between 1 and 7.

if...elseif...else...end if The if statement is a way, in ASP, of deciding what to do, given certain conditions. In the example on this page if is used to work out what day it currently is. An if is made up two parts the if and the then. if this statement is true then do this. An if can be extendeded with elseif (as in the example on this page). if this statement is true then do this elseif this is true then do this (elseif this is true then do this.... ad nauseam).

ifs often have a "catch all" statement at the end to be used if none of the previous ifs have worked. This is the else. All ifs finish with an end if

Usage:
if statement to check then
    do the following
elseif this statement to check then
    do the following instead
elseif this statement to check then
    do the following instead .....
else     if all else fails do this
end if

Advertisements


Popular Countries




Choose a letter