Response.Friend - An
insight into the Response Object
Introduction
The Response Object is one
of the most commonly used Objects in ASP. It contains methods like Write and
Redirect, along with properties like Buffer and Expires. This article will
provide you with the knowledge to use some of the most basic functions.
Note: When calling a method
in ASP you can use this : Response.Write "Hi", however in ASP.Net you must use Response.Write("Hi") By using the parenthesis it makes
it much easier to read. Parenthesis should always be used.
Response.Write
The Response.Write
is my 'most used method' as it simply prints text the screen. You obviously
wouldn't write a whole webpage with just Response.Write,
you mainly use it for custom statements, or in If
statements you need to quickly scribble a message to the screen.
Response.Write("Hi")
Response.Write("<font size=3>Welcome
" & name & "</font>")
Response.Write("<font size=3>Welcome to
my page</font>")
|
There are 3 very simple
statements. The first just displays the message 'Hi' the next one is a bit more
complicated, it sets a fontsize property and then
prints 'Welcome ' it then inserts a variable called name and then closes off
the font tag. The last one just uses the fontsize to
print 'Welcome to my page'.
The second one is one that
I will dwell on as you will use it a lot. When you use Response.Write, it is just as it you are writing
HTML, you can use all the tags and language you like, because in the end it
will appear exactly as you put it (but without Response.Write(" ")). Next, I put a variable into it
(this is assuming that I created the variable before and gave it a value), by
closing off the quotes you can either close the method or put an & to tell
it "I'm going to stick a variable here, get its value and add it to the
string", after I put name, I wanted to close the font tag, so I used &
again to add </font>.
If that was totally
confusing, look over it a few times and it will come to you.
Response.Redirect
Write is for writing, so
redirect is for __________ (Answer at end of document). To use Response.Redirect you use the same way as a method: Response.Redirect([place]). Here is an example:
Select Case page
Case "bar"
Response.Redirect("bar.asp)
Case "winecellar"
Response.Redirect("goto.asp?place="
& page)
End Select
|
If you didn't understand
the Select Case stuff then all I did there was work
out what was in the variable page and take the appropriate action. If page=bar then it used Response.Redirect to redirect to bar.asp.
However if page=winecellar it used it to redirect to goto.asp?place=winecellar, how did I get the winecellar? I used the &
to insert page, which just so happens to equal winecellar.
Response.Expires
Ok, Response.Expires is a property, not a method, so
instead of using Response.Expires(thing), you use Response.Expires = thing. What it sets is how long a page
will stay cached on the user's computer before it is deleted. This must come
before any HTML tags and usually is only set to 0 (not cached at all).
<% Response.Expires = 0 %>
<html>
.....
|
That was pretty self explanatory,
wasn't it?
Response.CacheControl
This is only used to tell
Proxy servers weather to cache the page or not.
Usually Proxy servers cache HTML pages to make it faster for users to get WebPages
(if they are using a proxy server), however, this doesn't work with ASP.
Because ASP pages are dynamic caching things like stock-prices or inventory
means that when you request the page again through a proxy server, it gives you
the same old cached information. By default the proxy will not cache an ASP
page (images, image maps, applets and other referenced items are cached as they
are static). But if you really want to let a proxy cache the page use this in
the page header.
<% Response.CacheControl = "Public" %>
<html>
.....
|
Nothing too fancy, this
lets all proxies to cache it (Public means everything).