(6 ratings)   
By: msconline.maconstate.edu
he manner in which Active Server Pages operates can be illustrated very simply by using VBScript functions to display the system date and time. It is assumed that you have basic familiarity with the Visual Basic language, of which VBScript is a...
Added: 25 June 2008    Views: 746  
PathComputers    Programming    Asp
Keywords: computers   programming   language   asp   database   functions   code   coder  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 

The following output reports the current date and time values from the server. This output is produced by an ASP script that embeds the values within the HTML on this page.

The current date is 6/25/2008 and the current time is 10:22:54 AM.

These date and time values are "live," meaning that they are retrieved from the server the instant the scripts are run. If you reload the page, you should see a different time reported.

The current date and time are retrieved from the server clock with the VBScript functions Date()Time(). Script references to these functions are references to the actual date and time values current on the server. and

Using the Response.Write() Method

Recall that one of the built-in ASP objects is the Response Object. This object handles script output and provides the Write() method to produce text output that appears on the Web page. So, the Response.Write() method can be coded inside a script at the location on the page where the system date and time should appear. It can write HTML text combined with the Date() and Time() values retrieved from the server. In fact, this is the method used to produce the live date and time values displayed above:

<%
Response.Write("<b>The current date is " & Date())
Response.Write(" and the current time is " & Time() & ".</b>")
%>

Note that the Response.Write() method can contain literal text strings (enclosed in quotes), values retrieved from VBScript functions or other VBScript commands, along with HTML tags (enclosed in quotes as part of a text string) to control output formatting. Text strings and server values are concatenated, or connected together, with the VBScript concatenation operator & (ampersand) to produce a resulting string of text characters that can be written to the Web page. The general format for the Response.Write() method is shown below:

Response.Write("text string" | & | server value | "<HTML tag>")

In the first line of the above script the literal text string "<b>The current date is " is concatenated with the server value produced by the Date() function to create the output string The current date is 6/25/2008. In the second line of the script the literal text string " and the current time is " is concatenated with the value of the Time() function and with the string ".</b>" to create the output string and the current time is 10:22:54 AM.

Notice that even though the script contains two lines of code, the output appears as a single line of text. This is because the output does not contain HTML code to produce a line-break character. You can, in fact, code the script with as many individual Response.Write() statements as you wish. The format of the output, however, depends solely on the HTML coding contained in the output string. Here is the same script written as multiple lines of code with one Response.Write() statement for each section of output:

<%
Response.Write("<b>")
Response.Write("The current date is ")
Response.Write(Date())
Response.Write(" and the current time is ")
Response.Write(Time())
Response.Write(".</b>")
%>

Concatenation of strings and values is not necessary in these statements since each Response.Write() statement outputs a single string or value. Alternately, the script can be written as a single line of code,

<%
Response.Write("<b>The current date is " & Date() & " and the current time is "
   & Time() & ".</b>")
%>

where various text strings and server values are concatenated to produce the output line.

Embedding Server Values within HTML

The Response.Write() method has a second, abbreviated format that makes it easy to embed server values within the HTML coded on the page. These scripts are in the following format,

<%= server value %>

where the symbols <%= and %> enclose a server-generated data value. This script is placed anywhere within the HTML coding on a page to display the value at that location on the page. The following output, for example,

The current date is 6/25/2008 and the current time is 10:22:54 AM.

is displayed with the following HTML code and embedded server values positioned at the above location:

<b>The current date is <%=Date()%> and the current time is <%=Time()%>.</b>

As the ASP processor renders the HTML code for return to the browser, it inserts the data values given by the Date() and Time() functions within the HTML code. This is a shorthand way of coding scripts that produce single data values and is an alternative to coding

<b>The current date is <% Response.Write(Date()) %> and the current time is <% Response.Write(Time()) %>.</b>

where a full script (even if a single line of code) is embedded on the page.

Whether you use the embed method of displaying server output by enclosing it between <%= %> Response.Write statements to display script output will generally be determined by your processing and display needs. If you are simply displaying a server value inside HTML that is already hard-coded on the page, you'll normally use the embed method. If your script needs to write HTML code as well as server values, then the Response.Write() symbols or whether you use method is preferred.

About the Author :
msconline.maconstate.edu
Displaying Server Values In ASP
Articles and Tutorials Directory by www.learnfobia.com
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments