 
| 4.0in 4.5in 5.0in 5.5in 6.0in 6.5in 7.0in 7.5in 8.0in 8.5in 9.0in 9.5in 10.0in 10.5in 11.0in 11.5in 12.0in 12.5in 13.0in 13.5in 14.0in 14.5in 15.0in 15.5in 16.0in 16.5in 17.0in 17.5in 18.0in 18.5in; mso-layout-grid-align: none">This
script displays the numbers 1 through 500 down the browser screen.
The output from the page is sent immediately to the browser after
each command in the page is executed.
You can watch the numbers appear down the screen in real time.
In
some situations, you may want to buffer the output of an Active Server
Page. When you buffer the
output of an Active Server Page, none of the page is sent to the browser
until the server has finished processing all of the page.
Here's a modified version of the preceding script:
<%Response.Buffer=True%>
<HTML>
<HEAD><TITLE>Buffer Example</TITLE></HEAD>
<BODY>
<%
FOR i=1 TO 500
Response.Write(i&"<BR>")
NEXT
%>
</BODY>
</HTML>
Only
one difference exists between this script and the previous one: In the
first line of this script, the Buffer property of the Response
object is set to True. When
this page is displayed in a Web browser, all the contents of the page are
sent to the browser at the same time.
The page is buffered until the script finishes processing.
Any
statement that modifies the Buffer Property must occur before any HTML or
script output. If you attempt
to modify the Buffer property after any HTML or script output, you'll get
an error.
By
buffering a page, you can display two different Web pages, depending on
some condition. For
example, the following Active Server Page randomly outputs two different
HTML pages:
<%
Response.Buffer=True %>
<HTML>
<MEAD><TITLE> First Page </TITLE></HEAD>
<BODY>
This is the first page.
</BODY>
</HTML>
<%
Randomize
If INT(2*RND)=l THEN Response.End
Response.Clear
%>
<HTML>
<HEAD><TITLE> Second Page </TITLE></HEAD>
<BODY>
This is
the second page.
</BODY>
</HTML>
In
this example, two new methods of the Response object are used: the End
method and the Clear method. The
End method immediately stops the processing of an Active Server Page and
outputs the results. You can
use the End method regardless of whether or not you're buffering the
output of a page. In this
example, the End method is used to prevent the second page from being
displayed when the first page is displayed.
The
clear method empties the current page buffer without outputting the
contents of the buffer. You
can use the Clear method only when buffering the output of an Active
Server Page. In this example,
the clear method is used to prevent the first page from being displayed
when the second page is displayed. It
clears the first page from the buffer.
One
other method of the Response object is used when buffering an Active
Server Page. The Flush method
immediately outputs the contents of the page buffer.
As with the Clear method, an error occurs if you attempt to use
this method with a page that isn't buffered.
Unlike the End method, after the Flush method is called, the page
continues to be processed.
Typically,
you won't need to buffer the output of an Active Server Page.
It's usually a bad idea. In
the case of large HTML pages or long-running scripts, buffering a page
delays the appearance of the Web page, which may confuse the user.
If
you want to display two different HTML pages conditionally, you can simply
use a VBScript conditional. For
example, here's the preceding example, rewritten without using buffering:
<%
Randomize
If INT(2*RND)=1 THEN
%>
<HTML>
<HEAD><TITLE> First Page </TITLE></HEAD>
<BODY>
This is the first page.
</BODY>
</HTML>
<% ELSE %>
<HTML>
<HEAD><TITLE> Second Page </TITLE></HEAD>
<BODY>
This is the second page.
</BODY>
</HTML>
<% END IF %>
The
one situation where buffering is necessary is when you need to change the
headers for a page within the body of the page.
If you want to change the headers in a page after you have already
outputted content to a browser, you need to set the Buffer property to
True. For example, later in
this chapter you learn how to use headers to control how a page is cached
and to specify the content rating of a page (see "Working with
Headers"). If you modify
either of these headers after you have already outputted content to a
browser, then you need to buffer the page.
Otherwise, you get an error.
 |