Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

State Management

Your IT Partner

Objectives
After completion of this chapter you will be able to: Understand Global.asax application file Explain different types of client-bases state management options Explain different types of server-side state management options

CMC Limited

Introduction
The Web is inherently stateless; each request for a page is treated as a new request, and information from one request is not available by default to the next request. To overcome this inherent limitation of Web-based applications, ASP.NET includes a number of features for managing statethat is, for storing information between requests. Use state management to track any piece of information or data that affects the behavior of the application.

CMC Limited

Introduction contd.. ASP.NET includes several options that help preserve data on both a per-page basis and an application-wide basis. These features are as follows: o View state o Session state o Control state o Profile Properties o Hidden fields o Cookies View state, control state, hidden fields, cookies, and query strings all involve storing data on the client in various ways. Application state, session state, and profile properties all store data in memory on the server.

CMC Limited

Global.asax File
The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level and session-level events raised by ASP.NET or by HTTP modules. The Global.asax file resides in the root directory of an ASP.NET application. At run time, Global.asax is parsed and compiled into a dynamically generated .NET Framework class derived from the HttpApplication base class. ASP.NET is configured so that any direct URL request for the Global.asax file is automatically rejected; external users cannot download or view the code in it.

CMC Limited

Client-Side State Management Options


The following section describes different types of client-bases state management options. o View state o Control state o Hidden fields o Cookies

CMC Limited

Client-Side State Management Options contd..

View State
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. When the page is processed, the current state of the page and controls is hashed into a string and saved in the page as a hidden field, or multiple hidden fields if the amount of data stored in the ViewState property exceeds the specified value in the MaxPageStateFieldLength property.

CMC Limited

Client-Side State Management Options contd.. When the page is posted back to the server, the page parses the view-state string at page initialization and restores property information in the page. You can store values in view state as well. The ViewState property is a dictionary that contains key/value pairs that contain the view-state data. When retrieving a value, you use the key name. You also need to cast the retrieved value to the appropriate data type using the casting syntax. This extra step is required because the ViewState collection stores all items as basic objects, which allows it to handle many different data types.

CMC Limited

Client-Side State Management Options contd..

Control State
The ASP.NET Framework includes a feature named Control State. Control State is very similar to View State. View State, Control State cannot be disabled. Control State is intended to be used only for storing crucial information across postbacks. Control State was introduced to address a problem that developers encountered in the first version of the ASP.NET Framework. Disable View State for any control by assigning the value False to a controls EnableViewState property.

CMC Limited

Client-Side State Management Options contd..

Cookies
A cookie is a small amount of data that is stored either in a text file on the client file system or inmemory in the client browser session. It contains site-specific information that the server sends to the client along with page output. Cookies can be temporary or persistent. Use cookies to store information about a particular client, session, or application. The cookies are saved on the client device, and when the browser requests a page, the client sends the information in the cookie along with the request information.

CMC Limited

Client-Side State Management Options contd.. The server can read the cookie and extract its value. A typical use is to store a token (perhaps encrypted) indicating that the user has already been authenticated in your application. Cookies are associated with a Web site, not with a specific page, so the browser and server will exchange cookie information no matter which page the user requests from your site. Properties of Cookie are given below: o Domain o Value o Expires o HasKeys o Name

CMC Limited

Server-Side State Management Options


The following section describes different types of server-side state management options.
o Session State o Using Profile

CMC Limited

Server-Side State Management Options contd..

Session State
ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. A Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests. ASP.NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to persist variable values for the duration of that session. By default, ASP.NET session state is enabled for all ASP.NET applications.

CMC Limited

Server-Side State Management Options contd..

Using Profile
The ASP.NET Framework provides you with an alternative to using cookies or Session state to store user information in the Profile object. The Profile object provides you with a strongly typed, persistent form of session state. Create a Profile by defining a list of Profile properties in your application root web configuration file. The ASP.NET Framework dynamically compiles a class that contains these properties in the background.

CMC Limited

Server-Side State Management Options contd.. When you define a Profile property, you can use any of the following attributes:

o o o o o o o o

Name type defaultValue readOnly serializeAs allowAnonymous provider customProviderData

CMC Limited

You might also like