Programming In Active Server Page (ASP) - Module One - Part 1

Started by sivaji, Jan 10, 2008, 04:44 PM

Previous topic - Next topic

sivaji

Programming Stuffs in ASP - Technical Skills for INTERVIEW

Module One

This module explains how to create ASP pages (.asp files) that use the fundamental features of HTML, ASP, and VBScript. This module includes the following lessons:
* Write and Run an ASP Page. Describes how to use Visual BasicĀ® Scripting Edition (VBScript) and HTML tags.
* Send Information by Using Forms. Shows how to display forms on an HTML page.
* Create a Guest Book. Uses forms to gather information from visitors, store the information in a database, and display the
   database contents in a Web page.
* Display an Excel Spreadsheet in ASP. Explains how to display an Excel spreadsheet in a Web page.

Write and Run an ASP Page

The best way to learn about ASP pages is to write your own. This lesson covers VBScript syntax and coding samples. To create an ASP page, use a text editor to insert script commands into an HTML page. Saving the page with an .asp file name extension tells the Web server how to process the script commands. To view the results of a script, request the page using a Web browser. VBScript is the default scripting language for ASP, and most of the examples in the tutorial are scripted in VBScript.

In HTML, you use brackets as delimiters around the tags:
<example>

In VBScript, you use the percent sign with brackets as delimiters around the tags:
<%example%>

You can put many tags inside one pair of VBScript delimiters:
<%example, samples%>

Example 1

This example displays the words "Hello World". To run this example, cut and paste it into an empty file and save it in the C:\Inetpub\Wwwroot\Tutorial directory as Example1.asp. Make sure to save your file with an .asp extension. Some text editors automatically change the file name extension to .txt when the Text Document option is selected in the Save dialog box. To prevent this, select the All Files(*.*) option. Exit your text editor as the server may not be able to display an HTML page that is open in a text editor. View your page with your browser by typing http://<Your Server Name>/Tutorial/Example1.asp in the address bar.

When running Example 1, the page is processed by the Web server in the following sequence:

1. Assigns the text "Hello World" to the variable FirstVar.
2. Uses HTML to make an HTML page.
3. Uses <%FirstVar%> to print out the value of the variable FirstVar.
4. Ends the HTML page.

<%@ Language=VBScript %>
<html>
<head>
<title>Example 1</title>
</head>
<body>
<%
FirstVar = "Hello world!"
%>
<%=FirstVar%>
</body>
</html>

Example 2

This example incorporates a FOR loop in the ASP page. The FOR loop is a statement that prints "Hello World" 10 times. To create Example 2, use the file from Example 1 and add the FOR loop code as shown in the following code sample. Save the file as Example2.asp. View it in your browser.

<%@ Language=VBScript %>
<html>
<head>
<title>Example 2</title>
</head>
<body>
<%
FirstVar = "Hello world!"
%>
<%FOR i=1 TO 10%>
<%=FirstVar%>
<%NEXT%>
</body>
</html>

Example 3

In this example, a time stamp is added to the ASP page. The word time is a function; it is a predefined VBScript function variable containing the current time. There are more than 90 functions in VBScript. Add the code for time shown in the following code sample to Example2.asp and save it as Example3.asp. View it in your browser.

<%@ Language=VBScript %>
<html>
<head>
<title>Example 3</title>
</head>
<body>
<%
FirstVar = "Hello world!"
%>
The time is: <%=time%> <BR>
<%FOR i=1 TO 10%>
<%=FirstVar%>
<%NEXT%>
</body>
</html>

Example 4

This example displays the message "Good Morning Everyone" if the hour is between 4:00 A.M. and 6:00 P.M. It displays the message "Good Night Everyone" if the hour is between 6:00 P.M. and 4:00 A.M. Add the IF THEN statement in the code shown below to Example3.asp and save it as Example4.asp.

<%@ Language=VBScript %>
<html>
<head>
<title>Example 4</title>
</head>
<body>
<%IF Hour(time)>18 OR Hour(time)<4 THEN%>
Good Night Everyone.
<%ELSE%>
Good Morning Everyone.
<%END IF%>
</body>
</html>

Send Information by Using Forms

A common use of intranet and Internet server applications is to process a form submitted by a browser. With ASP, you can embed scripts written in VBScript directly into an HTML file to process the form. ASP processes the script commands and returns the results to the browser. In this lesson, you create an HTML page that displays various elements of an HTML form. Later in this module, you use this knowledge of forms to build a guest book application on your Web site.

This lesson contains the following examples:
    *  Button example. Displays selection buttons in the form.
    *  Text example. Displays text boxes in the form.

Button example

In this example, there are three input lines that use "buttons," and two default buttons-RESET and SUBMIT. The Post method is used to send data from the client browser to the Web server. Open your text editor, create a new file, and paste in the following code. Save the file as Button.htm and view the page in your browser.

<html>
<head>
<title>Button Form</title>
</head>
<body>
<FORM NAME="Button Example" METHOD="POST" ACTION="tutorial/button.htm">
Computer Programming Experience:
<P>
<INPUT TYPE="button" NAME="choice" VALUE="Less than 1">Less
   than 1 year.<BR>
<INPUT TYPE="button" NAME="choice" VALUE="1 to 5">1-5 years.<BR>
<INPUT TYPE="button" NAME="choice" VALUE="More than 5">More
   than 5 years.
</P>
<P><INPUT TYPE="reset" VALUE="Clear Form">
<INPUT TYPE="submit" VALUE="Submit">
</P>
</form>
</body>
</html>

Text example

In this example, you create text fields in a form. Open a new file in your text editor, paste in the following code, and save the file as Text.htm:

<html>
<head>
<title>Text Form</title>
</head>
<body>
<FORM NAME="Text Example" FORM METHOD="POST" ACTION="tutorial/text.htm">
<TABLE>
<TR>
<TD ALIGN="RIGHT" VALIGN="MIDDLE">Name?
<TD ALIGN="LEFT">
<INPUT TYPE="text" NAME="name" VALUE=""
          SIZE="20" MAXLENGTH="150">
<TR>
<TD ALIGN="RIGHT" VALIGN="MIDDLE">Company?
<TD ALIGN="LEFT">
<INPUT TYPE="text" NAME="company" VALUE=""
          SIZE="25" MAXLENGTH="150">
<TR>
<TD ALIGN="RIGHT" VALIGN="MIDDLE">
Email Address?
<TD ALIGN="LEFT">
<INPUT TYPE="text" NAME="email" VALUE=""
          SIZE="25" MAXLENGHT="150">
</TABLE>
<INPUT TYPE="reset">
<INPUT TYPE="Submit" NAME="Submit" VALUE="Submit">
</form>
</body>
</html>
Am now @ Chennai