Active Server Page Architecture

Active Server Page Syntax

Server-side script is contained in either <% %> delimiters, or in a <SCRIPT> tag. When server-side script is used in the <SCRIPT> tag, you must include a reference to the RUNAT attribute. The RUNAT attribute specifies whether this scripting code should be run on the server or on the client. By default, script found in a <SCRIPT> tag will be reserved for execution in the client Web browser.

The <% %> Delimiters

When a Web server processes ASP, it runs any script code between the <% and %> delimiters. These delimiters are used to separate the script code from the HTML in a Web page.

In general, an ASP contains HTML code mixed with server-side script. The server-side script programmatically determines what information will be returned to the user .

Example

This example uses the Now and Hour procedures contained in the <% and %> delimiters to determine the current time, and then greet the user with either "Good Morning" or "Good Day," depending on the time:
 <% if Hour(Now) < 12 then %> Good Morning. <% else %> Good Day. <% end if %> 

If it is 8:00 A.M., the HTML returned to the user will be:

 Good Morning. 

Example

This example uses VBScript to test the value of a variable in an ASP file:
 <%Dim MyVar MyVar = 3 Select Case MyVar Case 1 %><BOLD>The value is one.</BOLD><% Case 2 %><BOLD>The value is two.</BOLD><% End Select%> 

Displaying Output

To display HTML to the user with output from the script, use the <%= %> syntax.

Example

This example displays the current time to the user:

 The time here is now <%= Time %>. 

If it is 8:34 A.M., the HTML returned to the user is:

 The time here is now 8:34 AM. 

The <SCRIPT> Tag

You can also add server-side script to an HTML <SCRIPT> tag by setting the RUNAT attribute to Server . In a <SCRIPT> section, you can create server-side functions and sub procedures that can be invoked from other scripts on the page.

Example

This example uses server-side script to determine whether it is morning or afternoon:
 <SCRIPT LANGUAGE=VBScript RUNAT=SERVER> Function ComputeAMPM() If Hour(Now) < 12 Then  ComputeAMPM = "morning" Else ComputeAMPM = "afternoon" End If End Function </SCRIPT> 

You can display the result directly from the HTML <SCRIPT> section by using the Response.Write method.

Example

This example displays a message based on the time of day:
 <SCRIPT LANGUAGE=VBScript RUNAT=SERVER> Response.Write "Time for your " & ComputeAMPM() & " classes." </SCRIPT> 

If it is 6:00 A.M., the HTML returned to the user will be:

 Time for your morning classes. 

In the <SCRIPT> section, any code that is not contained in a procedure runs when the Web server processes the .asp file. Code in a procedure will not run until the procedure is explicitly invoked by server-side script.



Microsoft Windows Architecture Training
Microsoft Windows Architecture for Developers Training Kit
ISBN: B00007FY9D
EAN: N/A
Year: 1998
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net