90 likes | 194 Views
State Management 2. In ASP. Net. View State. View state uses a hidden field when the form is posted to store state on behalf of a client. Each time a page is posted to itself, the contents of the __VIEWSTATE field are sent as part of the post.
E N D
State Management 2 In ASP. Net
View State • View state uses a hidden field when the form is posted to store state on behalf of a client. • Each time a page is posted to itself, the contents of the __VIEWSTATE field are sent as part of the post. • The primary use of view state is for controls to retain their state across post-backs, but it can also be used as a mechanism for storing generic client-specific state between post-backs to the same page. • You can safely access the ViewState in your Load event handler.
Application and Session Objects – Reloaded. • Application – contains a collection of data that is available across the entire application during the life of the application. • Session – contains data that is available across all pages accessed by a single user during a single session. • You can write code to respond to Application and Session events in the global.asax file.
Session State • Session state is maintained on a per-client basis. • When a client first accesses any page in an application, an ASP.NET generated session ID is created. • Session state is used for things like: • Shopping carts, Viewing preferences, other • Session state is the most flexible and, in general, the most efficient means of maintaining client-specific state.
Using the Session Object • Session state is maintained on behalf of each client within an ASP.NET application. • When a new client begins to interact with the application, a new session ID (or session key) is generated • The ID is associated with all subsequent requests from that same client. • The state is retained in memory on the server in the default session state configuration. • By default, the session key is retained on the client side in a cookie. (there is an alternative in cases where cookies do not work)
Application State • Application state is where information that is global to the application may be stored. • For efficiency, this state is typically stored once and then read from many times. • Often used for application statistics variables or constant global data: • Number of users who have accessed site • Counts of different types of browsers • Can prefetch static data from a database or file • Should be used with care
Using the Application Object • Application object is implicitly locked for you when data is read from or written to it. • May make more sense to avoid using application object and use data cache if you are just storing global constants. • Application object must be used if you are using shared, updatable data.