Asp.net state management system tutorial


Asp.net state management system tutorial - contributed by Nihal Singh

.NET Tutorial > ASP.NET State Management

ASP.NET State Management

HTTP (Hyper Text Transfer Protocol) is a stateless protocol, which means a new instance of the web page class is re-created each time the page is posted to the server. It is a communication protocol which is implemented in the "World Wide Web (WWW)". It is a request/response style protocol. Clients (browsers) will request to a server (web server) and the server responds to these requests. Since HTTP is stateless, managing state in web applications is challenging. State management techniques are used to maintain user state throughout the application. The following are the commonly used state management techniques.

  • View state
  • Control state
  • Hidden fields
  • Cookies
  • Query strings
  • Application state
  • Session state
  • Profile Properties

Storing information on the client has the following advantages:

Better scalability. If a Web site has thousands (or lacks) of simultaneous users, the memory consumed by storing state management information can be limited. Storing the information to the clients removes this problem and frees the server to use its resources to serve more requests.

Supports multiple Web servers: With client-side state management, you can distribute
Incoming requests across multiple Web servers (or a Web farm).

Storing information on the server has the following advantages:

Better security Client-side state management information can be captured and maliciously modified. Therefore, we should never use client-side state management to store confidential information such as a password, authorization level, or authentication status.

Reduced bandwidth If you store large amounts of state management information, sending that information from client to server can increase bandwidth utilization and page load times, potentially increasing your costs and reducing scalability.

Client Based State Management

View state, control state, hidden fields, cookies, and query strings all stores the data on the client in various ways.

View State

The Page.ViewState property provides a dictionary object for storing values between multiple requests for the same page. The page uses this method to preserve page and control property values between round trips.

Example:

//Writing to view state

ViewState.Add("MyVar", "CarrerRide");

//Read from view state

String myData = (string) ViewState ["MyVar"];

ViewState data available only within the page not on the other page.

Control State

The ControlState property allows you to persist property information that is specific to a control and cannot be turned off like the ViewState property.

HiddenField

You can store information in a HiddenField control, which renders as a standard HTML hidden field. A hidden field does not visible in the browser, but you can set its properties just as you can with a standard control. Like view state, hidden fields only store information for a single page. Therefore, they are not useful for storing session data that is used between page requests.

Cookies

A cookie is a small amount of data that is stored either in a text file on the client file system or in-memory in the client browser session. It contains site-specific information that the server sends to the client along with page output.

Server Based State Management

Application state, session state, and profile properties all store data in memory on the server.

Application State

Application state is an instance of the HttpApplicationState class. It is a global storage mechanism .Thus you can access Application state data from all pages in the Web application. So, application state is useful for storing information that needs to be maintained between server round trips and between requests for pages

Storing and retrieving data

Application ["Message"] = "Welcome to the CarrerRide.”

If (Application ["Message"]! = null)
{
Supports multiple Web serversString message = (String) Application ["Message"];
}

Application Events

Application_Start : The Application_Start event is raised when your application is started first time. This event is useful for initializing variables that are scoped at the application level.
Application_End : The Application_End event is raised when your application stops
Application_Error : It is raised when an unhandled error occurs
Application_LogRequest: This event is raised when a request has been made to the application.
Application_PostLogRequest: It is raised after the logging of a request has completed.

Session State

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages. It is secure, transparent from users and we can store any kind of object within it. Session object is an instance of the HttpSessionState class. It is scoped to the current browser session.

Advantages and Disadvantages of Session.

Advantages:

  • It helps to maintain user states and data to all over the application.
  • We can store any kind of object.
  • Stores every client data separately.
  • Session is secure and transparent from user.

Disadvantages:

  • Performance overhead in case of large volume of user, because of session data stored in server memory.
  • Overhead involved in serializing and De-Serializing session Data. Because In case of StateServer and SQLServer session mode we need to serialize the object before store.

Session Mode

The following session modes are available in ASP.NET.

  • InProc
  • StateServer
  • SQLServer
  • Custom

Storing and retrieving values from Session

//Storing UserName in Session

Session["UserName"] = txtEmpName.Text;

//Check weather session variable null or not

if (Session["UserName"] != null)
{
            //Retrieving EmployeeName from Session
            lblEmp.Text = "Welcome: " + Session ["UserName"];
}
else
{
            //Do something
}

Profile Properties

Profile allows you to store user-specific data. Profile data is not lost when a user's session expires.

To use profiles, you first enable profiles by modifying the Web.Config file for your ASP.NET Web application

<profile>
<properties>
<add name="CarrerRide" />
</properties>
</profile>



Write your comment - Share Knowledge and Experience



 
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring