0% found this document useful (0 votes)
2 views

Chapter7 _State_ Management (2)

The document discusses state management in ASP.NET, which allows the preservation of user information across multiple page requests and postbacks. It outlines two types of state management: server-side and client-side, detailing their respective methods and security implications. Additionally, it explains the use of application and session states, as well as cookies for maintaining state information.

Uploaded by

somaliyow17
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter7 _State_ Management (2)

The document discusses state management in ASP.NET, which allows the preservation of user information across multiple page requests and postbacks. It outlines two types of state management: server-side and client-side, detailing their respective methods and security implications. Additionally, it explains the use of application and session states, as well as cookies for maintaining state information.

Uploaded by

somaliyow17
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Web Design & Development

I
(ASP.NET)
CHAPTER7. State Management

LECTURER :MOHAMED ABDI DAGANE


INTRODUCING STATE
MANAGEMENT
 ASP.NET provides state management that
saves information on the server between
page requests and postbacks; this helps to
maintain the continuity of user information
(state) throughout a visit to a Web site
 If state is maintained between pages, you

can reuse the information so that users do


not have to reenter the same information
multiple times.
 This is particularly useful during the
postback process when a user fills out a
long or complicated form.
INTRODUCING STATE
MANAGEMENT
Without State With State
Management Management

Login.aspx Login.aspx
Please enter your Please enter your
logon information: logon information:
First Name First Name

John John
Last Name Last Name
Chen Chen

Submit
Web Submit Web
Web Server
Server
Submit Web Server
Server Submit
Greetings.aspx Greetings.aspx

Hello Hello John Chen

II forget
forget who
who you
you
are!!
are!!
INTRODUCING STATE MANAGEMENT
 Any Hypertext Transfer Protocol
(HTTP)–based technology, Web Forms
are stateless, so each request from the
client browser to the web server is
understood as an independent request.
 This means, they do not automatically

indicate if the requests in a sequence are


all from the same client or even if a
single browser instance is still actively
viewing a Web page or a Web site.
WHAT IS STATE MANAGEMENT?
 State management is the process by
which you maintain the same information
throughout multiple requests for the
same or different Web pages.
 It is technique used to maintain state

information for ASP.NET web pages


across multiple requests.
TYPES OF STATE MANAGEMENT
 ASP.NET provides two types of state
management:
1.Server-side state management options
use server resources to store state
information. These options provide higher
security than client-side state
management.
2. Client-side state management does not
use server resources to store state
information and therefore offers fast server
performance. However, it can be unreliable
and pose a security risk if you want to
store sensitive information.
TYPES OF STATE MANAGEMENT

Server-Side State Client-Side State


Management Management

Application state Cookies


• Information is available to all • Plain text file that stores
users of a Web application information to maintain state

Session state ViewState and Control State


• Information is available only to a • Retains values between multiple
user of a specific session requests for the same page

Database Query strings


• In some cases, use database
• Information appended to the end
support to maintain state on your
of a URL
Web site
SERVER-SIDE STATE
MANAGEMENT
APPLICATION OBJECT
 Application state is a global storage
mechanism accessible from all pages in
the Web application
 ASP.NET creates an HttpApplication object

named Application whenever your


application starts—that is, as soon as
someone requests any page from your
site for the first time.
 Only one Application object is created for

your entire application


SERVER-SIDE STATE
MANAGEMENT
APPLICATION VARIABLES
 ASP.NET provides the application variables

that maintain application state.


 These application variables are available

across your entire application and don’t


apply to any specific user.
 For example, suppose you have a footer or

disclaimer that you need to place on every


single page. You can store this in an
application variable with the following:
Application(“Disclaimer”) = “Copyright
2001
SERVER-SIDE STATE
MANAGEMENT

SESSION STATE
 Session state is limited to the current browser
session:
 Values are preserved through the use of application
and session variables
 Scalability
 An ASP.NET session is identified by the SessionID
string
Web
Web Server
Server

Client
Client Computer
Computer

SessionID Application and


Session variables
SERVER-SIDE STATE MANAGEMENT
SESSION STATE
 When a user visits your site, he’s allocated

a “locker” into which the developer can put


whatever information she likes. The user’s
time at the site is called a session . Once
the user leaves the site, the locker is
abandoned, the information is lost, and the
session ends. The syntax is as follows:
Session.Add(variablename, value)
Or:
Session( variablename) = value;
CLIENT-SIDE STATE MANAGEMENT
COOKIES
 A cookie is a small amount of data that is

stored either in a text file on the file


system of the client computer or in-
memory in the client-browser session.
The server sends the page output and a
cookie that contains page-specific
information to the client.
 You can use cookies to store information

about a particular client, session, or


application
TYPES OF COOKIES
 The two types of cookies are:
1. Temporary. Temporary cookies, also
called the session or non-persistent cookies,
exist only in the memory of the browser.
When the browser is shut down, any
temporary cookies that were added to the
browser are lost.
2. Persistent. Persistent cookies are similar
to temporary cookies except that persistent
cookies have a definite expiration period.
When a browser requests a page that
creates a persistent cookie, the browser
saves that cookie to the user’s hard disk.
CREATING COOKIES
 TheResponse object allows you to create
cookies easily. There are two ways to
create cookies: You can create multiple
cookies, each with a single value, or
you can create a single cookie with
multiple key/value pairs. The following
code snippet demonstrates both methods.
Response.Cookies(“MyCookie”).Value
= “Single Cookie”
Response.Cookies(“21DaysCookie”)
(“Username”) = “Chris”
Response.Cookies(“21DaysCookie”)
(“Preference”) = “800x640”
ACCESSING COOKIES
The browser sends all cookie information to the
server when it makes a request. Therefore, you
can use the Request object to gather that
information. Accessing a cookie follows the
exact same syntax as creating a cookie. In the
following listing, you’ll use Response.Write to
write the cookie values to the browser
Response.Write( _
Request.Cookies(“MyCookie”).Value)
Response.Write( _
Request.Cookies(“21DaysCookie”)(“Username”))
Response.Write( _
Request.Cookies(“21DaysCookie”)(“Preference”));
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
 Dim strVariable As String

 'set up some cookie variables


 Response.Cookies("temp").Value = "HI"
 Response.Cookies("21DaysCookie")("Username") = "Chris"
 Response.Cookies("21DaysCookie")("Preference") = _
 "800x640"
 Response.Cookies("21DaysCookie")("Password") = _
 "CookiesRock"
 Response.Cookies("21DaysCookie")("LastVisit") = _
 DateTime.Now.ToString
 Response.Cookies("21DaysCookie")("UserAgent") = _
 Request.ServerVariables("HTTP_USER_AGENT")

 For Each strVariable In Response.Cookies _


 ("21DaysCookie").Values
 Label1.Text += "<b>" & strVariable & "</b>: " & _
 Request.Cookies("21DaysCookie")(strVariable) & "<br>"
 Next
 End Sub

You might also like