Developing ASP.NET Web Applications
(Page 1 of 4 )
In this second part of a three-part series on ASP.Net web programming, you'll learn how state is handled in ASP.Net web applications, the components of the .NET framework, and more. This article is excerpted from chapter one of
Murach's ASP.NET 3.5 Web Programming with VB 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774472).
How state is handled in ASP.NET applications
Although it isn’t apparent in the previous figure, an application ends after it generates a web page. That means that the current status of any data maintained by the application, such as variables or control properties, is lost. In other words, HTTP doesn’t maintain the state of the application. This is illustrated in figure 1-5.
Here, you can see that a browser on a client requests a page from a web server. After the server processes the request and returns the page to the browser, it drops the connection. Then, if the browser makes additional requests, the server has no way to associate the browser with its previous requests. Because of that, HTTP is known as a stateless protocol.
Although HTTP doesn’t maintain state, ASP.NET provides several ways to do that, as summarized in this figure. First, you can use view state to maintain the values of server control properties. For example, you can use view state to preserve the Text properties of label controls that are changed as a web form is processed or to maintain a list of items in a drop-down list. Because ASP.NET implements view state by default, you don’t need to write any special code to use it.
Second, you can use session state to maintain data between executions of an application. To make this work, ASP.NET creates a session state object that is kept on the server whenever a user starts a new session. This session object contains a unique session ID, and this ID is sent back and forth between the server and the browser each time the user requests a page. Then, when the server receives a new request from a user, it can retrieve the right session object for that user. In the code for your web forms, you can add data items to the session object so their previous values are available each time a web form is executed.
Third, you can use an application state object to save application state data, which applies to all of the users of an application. For example, you can use application state to maintain global counters or to maintain a list of the users who are currently logged on to an application.
Fourth, you can use the profile feature to keep track of user data. Although a profile is similar to a session state object, it persists between user sessions because it is stored in a database. When you use profiles, for example, you can keep track of the last three products that a user looked at in his last session. Then, when the user starts a new session, you can display those products in a “Recently viewed items” list.
Why state is difficult to track in web applications

Concepts
State refers to the current status of the properties, variables, and other data maintained by an application for a single user. The application must maintain a separate state for each user currently accessing the application.
HTTP is a stateless protocol. That means that it doesn’t keep track of state between round trips. Once a browser makes a request and receives a response, the application terminates and its state is lost.
Four ASP.NET features for maintaining state
ASP.NET uses view state to maintain the value of form control properties that the application changes between executions of the application. Since view state is implemented by default, no special coding is required.
When a user starts a new session, ASP.NET creates a session state object that contains a session ID. This ID is passed from the server to the browser and back to the server so the server can associate the browser with an existing session. To maintain session state, you can add program values to the session state object.
When an application begins, ASP.NET creates an application state object. To maintain application state, you can add program values to the application state object. These values are available to all users of the application until the application ends.
ASP.NET can also maintain a profile for each user of an application. Because profiles are stored in a database, the profile data is maintained from one user session to another. This makes it easier to personalize an application.
Figure 1-5 How state is handled in ASP.NET applications
Next: An introduction to ASP.NET application development >>
More ASP.NET Articles
More By Murach Publishing
|
This article is an excerpt from chapter one of Murach's ASP.NET 3.5 Web Programming with VB 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774472). Check it out today at your favorite bookstore. Buy this book now.
|
|