0% found this document useful (0 votes)
76 views9 pages

State Management 2: in ASP. Net

The document discusses state management in ASP.NET using ViewState, Session, and Application objects. ViewState stores client-specific state on the server between postbacks using a hidden field. Session stores data available to a single user across multiple pages during a browsing session. Application stores global data available to all users for the life of the application. Events allow responding to state changes.

Uploaded by

Hemanth Vusirika
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views9 pages

State Management 2: in ASP. Net

The document discusses state management in ASP.NET using ViewState, Session, and Application objects. ViewState stores client-specific state on the server between postbacks using a hidden field. Session stores data available to a single user across multiple pages during a browsing session. Application stores global data available to all users for the life of the application. Events allow responding to state changes.

Uploaded by

Hemanth Vusirika
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

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.
Session Events

Event Description
Session_OnStart Occurs when a new user accesses a page
that’s part of the application. Can be used to
initialize: session variables, session level
objects, and begin database connections.
Session_OnEnd Happens when a session times out. Can be
used to any final cleanup and to close a
database connection.
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
Application Events
Event Description

Application_OnStart Happens once – when the first user access the


app. Can be used to retrieve or initialize
information that will be used across all
sessions.
Application_OnEnd Happens once – when the last user leaves the
app. Can be used to clean up any app-level
variables and objects.
Application_OnBeginRequ Happens every time a page in the application
est is requested, before the request is serviced.

Application_OnEndReques Happens after each request is serviced. The


t last event that can have an effect on the
response.
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.

You might also like