HTTP is stateless. An HTTP Request is received by the web server. It hands
it off the the ASP.Net application. The application identifies the handler
for that Request (a Page class). The Page class is instantiated and handles
the Request. It sends a Response to the client. Then it goes out of scope,
off into NothingLand. The next Request comes for a Page. The same thing
happens again.
Now, as HTTP is stateless, ASP.Net includes some tricks for persisting state
across Page Requests. One is the PostBack. When the PostBack is received, it
contains the values in the Page, which are used to re-build the Page class
on the server in the state it was on the client. Without it, the Page form
would be completely empty of values, as HTTP is stateless. Another is
ViewState. ViewState uses a hidden form field which is inserted into the
Page when the Response is sent to the browser. It contains the current state
of all the form elements in the Page. When the Page Posts back, the
ViewState is read, and the new Page instance is re-populated with the saved
state of the Page coming from the POST Request.
Note that this enables ONE PAGE class to remember the state IT was in prior
to a PostBack. So, how do you persist data across multiple pages? After all,
HTTP IS STATELESS (I'm going to keep repeating this so that you will get it
drummed into your head - it is VERY important). Well, HTML has a mechanism
for remembering a few things. It's called Cookies. So, when the first
Request for a Page comes from any client, the Session Collection has a new
member added to it, with a unique ID, a Guid, that identifies the client
that sent the Request. This Guid is sent back to the client in the
Response.Cookies collection, as a Session Cookie (it is not saved on the
client, except during the current Session). Every time the browser makes a
Request, it sends its Cookies in the Request header. The server reads the
Session Cookie, and uses the Session object for that client during that
Request/Response.
So, are there any other ways to save the state of a User Control across
multiple page requests? Sure, but they all involve one thing: The client
must be uniquely identified on the server, so that the Page handling the
Request, which remembers nothing about any previous Requests (because HTTP
IS STATELESS), can identify what client data to fetch for that Page. How to
do that? Well, a Cookie comes to mind. You could create a Guid on the
server, and pass it to the client in the Response.Cookies Collection, and
read it every time a new Request comes back. You would have to store both
the Cookie Guid, and the User Control data somewhere. Now, you obviously
don't want to store it in memory, or you would probably use Session. You
could store it in a database, though, for example. Of course, as HTTP IS
STATELESS, and the server has no way to know when the client user has lost
interest and navigated somewhere else, you would have to include some sort
of Timeout process to remove the data from the database after a specified
interval with no Requests.
This is, of course, how Session works. So you don't have to.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.