You are on page 1of 24

ASP NET St

ASP.NET State
t MManagementt

http://freepdf-books.com
State Management
` How is a traditional desktop application different from a
web application?
` Thousands of users simultaneously run the same
application on one computer (the web server)
` Each one communicating over a stateless HTTP
connection.
` These conditions make it impossible to design a web
application like a traditional Windows program.
` This information can be as simple as a user’s name
name, or as
complex as a stuffed‐full shopping cart for an e‐
commerce store.

2
http://freepdf-books.com
ASP.Net State Management
` Client Based State Management
` View state
` Hidden fields
` Cookies
` Query strings
` Server Based State Management
` Application state
` Session state
` Profile Properties

3
http://freepdf-books.com
Client Based State Management

4
http://freepdf-books.com
1. Viewstate
` The ViewState property provides a dictionary object for retaining values between
multiple requests for the same page.
` This is the default method that the page uses to preserve page and control
property values between round trips.
` Encoded in hidden form field

` Mayy add to viewstate p


programmatically
g y
` ViewState[“UserID”] = “333”;

` Retrieve with same syntax


` Returned datatype is object
` Must convert

` int UserID = Convert.ToInt32(ViewState[


Convert ToInt32(ViewState["UserID"]);
UserID ]);
` Int UserID = (int) ViewState["UserID"];

http://freepdf-books.com
Viewstate
` Bandwidth issues
` Viewstate can get quite large
` Disable when not needed:
` EnableViewState = “false”
` Disable individual controls,, page,
p g , entire application
pp
` Security
` Default: viewstate is encoded (not encrypted – just a
base64string!)
` Not secure
` Benefits:
` V convenient
Very i t
` Drawback
` Difficult to transfer between pages
p g
` Does not persist between sessions

http://freepdf-books.com
2. Hidden Fields
` The HiddenField control is used to store a value that
needs to be persisted across posts to the server. It is
rendered as an <input type= "hidden"/> element.
` Normally view state, session state, and cookies are used
to maintain the state of a Web Forms page. However, if
these methods are disabled or are not available, you can
use the HiddenField control to store state values
values.
` To specify the value for a HiddenField control
` use the Value property.
` To monitor the change in value of the HiddenField control
` Create an event handler for the ValueChanged event.

7
http://freepdf-books.com
3. Query String
` Query strings are an easy way to pass information
from one page to another or even back to the same
page

` Response.Redirect("newpage.aspx?recordID=10");
` Response.Redirect("newpage.aspx?recordID=10&mode=full");
` string ID = Request.QueryString["recordID"];

` Some browsers and client devices impose a 2083‐


character limit on the length of the URL.

8
http://freepdf-books.com
4. Cookies
` Cookies are a client‐side approach for
persisting state information.
information

` They are name‐value pairs that are saved


within one or more files that are managed by
the browser.

` These pairs accompany both server requests


p
and responses within the HTTP header
9
http://freepdf-books.com
Cookies

10
http://freepdf-books.com
Cookies
` Cookies are not associated with a specific page but
with the page
page’ss domain
domain, so the browser and server
exchanges cookie information no matter what page
the user requests
q from the site.

` The browser managesg the cookies for the different


domains so that one domain’s cookies are not
transported to a different domain.

11
http://freepdf-books.com
Cookies
` Namespace:
` Namespace ‐ using System.Net;

` S i l “f
Special “feature”
t ” off ASP
ASP.NET
NET cookies
ki
` Cannot add new key to existing cookie
` Must copy
py cookie items to code
` Add new item
` Write new cookie

` Expiration:
` Default: when browser is closed

http://freepdf-books.com
Cookies

13
http://freepdf-books.com
Cookies
` Benefits:
` Persist
P i tbbetween
t sessions
i
` Keep track of usernames, last visit, etc.
` E
Easy tto use

` Drawbacks:
` Client can block
` Client can view, edit, delete
` Not secure

http://freepdf-books.com
Server Based State Management

15
http://freepdf-books.com
1. Application State
` Application state is a server‐stored state mechanism that
allows you to store global data in a dictionary
dictionary‐style
style
collection that is accessible from all pages in the Web
application.
` It is stored in server memory, but unlike session state
(covered shortly), which is specific to a single user
browser session,
session application state applies to all users and
sessions of the Web application.
` Thus, application state is ideal for storing relatively small,
frequently used sets of data that do not change from
user‐to‐user or request‐to‐request.

16
http://freepdf-books.com
Application State
` Items placed in application state remain there until the
application restarts.

` Add data
` Application[“UserCount”] = (int)Application[“UserCount”]+ 1;

Best Place to initialize in Application_Start() in Global.asax

http://freepdf-books.com
Application State
` When should items in application state be incremented?
` If you are counting the number of users visiting on your
website then you should increment your Application variable
while a new session is started.
` So Session_Start()
() in Global.asax
l b l is the
h bbest place
l to
increment

18
http://freepdf-books.com
2. Session State
` Session state is a server‐based state mechanism that
allows you to store data in a dictionary
dictionary‐style
style collection.
` It is scoped to the current browser session.
` That is, each user or browser session has a different session state.
` Session state only lasts for a limited finite amount of time
(usually around 20 minutes).
` Thus, if a user leaves the application and then returns later
Thus later, the
second user session has a different session state from the first.
` Session.Timeout = 60;;

19
http://freepdf-books.com
Session State
` Session state is typically used for storing information that
needs to be preserved across multiple requests by the
same user.
` Because each user session has its own session state
collection, it should not be used to store large chunks of
information
` this consumes very llarge amounts
thi t off server memory as lloads
d
increase.

20
http://freepdf-books.com
Session State
` Because session information does eventually timeout, you
should always y check if an item retrieved from session state still
exists before using the retrieved object.
` If the session object does not yet exist (either because it is the
fi t ti
first time th
the user h
has requested
t d it or b
because th
the session
i h has
timed out), one might generate an error, redirect to another
page, or create the required object using lazy initialization.

21
http://freepdf-books.com
3. Profile Properties
` ASP.NET provides a feature called profile properties, which
allows you to store user‐specific data. This feature is similar to
session state
state, except that the profile data is not lost when a
user's session expires.
` The profile‐properties feature uses an ASP.NET profile, which is
stored in a persistent format and associated with an individual
user. The ASP.NET profile allows you to easily manage user
information without requiring you to create and maintain your
own database.
` In addition, the profile makes the user information available
using a strongly typed API that you can access from anywhere
in yyour application.
pp You can store objects
j of anyy type
yp in the
profile. The ASP.NET profile feature provides a generic storage
system that allows you to define and maintain almost any kind
of data while still making the data available in a type‐safe
manner.
22
http://freepdf-books.com
Profile Properties
` To use profile properties, you must configure a profile
provider. ASP.NET includes a SqlProfileProvider class that
allows you to store profile data in a SQL database, but
you can also create your own profile provider class that
stores profile data in a custom format and to a custom
storage mechanism such as an XML file, or even to a web
service.
service
` Because data that is placed in profile properties is not
stored in application
pp memory,y it is preserved
p through
g
Internet Information Services (IIS) restarts and worker-
process restarts without losing data.

23
http://freepdf-books.com
The End.

24
http://freepdf-books.com

You might also like