ASP.NET, Application state Management, Query string, Session,Application, Cookies

When you build dynamic Applications you need information between client requests. But HTTP is stateless protocol which means you need to maintain state of the controls in some other way.
What is state management?

State management is a process by which you maintain state and information of page (include all controls) between multiple client requests.

There are two Types of state management in ASP.NET.

Those are
1. Client side state management.
2. Server side state management.

Client side state management:
Client side state management means to store the values at client side.
We can maintain state at client side through Query string, viewstate and cookies.

Query string: Query string is the data that is append to the end of the URL. Query strings provide a simple but limited way to maintain state information.

For example if you want to pass information from one page to another, such as passing a id number from one page to another page where it will be processed.

In order for query string values to be available during page processing, you must submit the page using an HTTP GET command. That is, you cannot take advantage of a query string if a page is processed in response to an HTTP POST command.

E.g. /page.aspx?id=2

ViewState: The view state retains values of the controls between multiple requests for the same page.

In Asp.Net all server control values are encoded and stored in the viewstate variable called __VIEWSTAE before responding to the server request.

For all server controls in ASP.NET viewstate property set to True by default.

Even you can create viewstate variable by assigning the value to viewstate.
Viewstate(“id”)=1027
The view state is a key element of an ASP.NET page because it is the primary means to persist the state of the Web server controls. Whenever the page posts back, the state is restored, updated using the current form parameters, then used to run the postback event handler. Normally, the view state is a hashed string encoded as Base64 and stored in a hidden field called __VIEWSTATE.

Cookies: Cookies also used to maintain the state at client site(that means at browser). Cookie is a small text file that is stored on client browser.

There are two types of cookies.
1) Persistent Cookies
2) Non Persistent Cookies
Persistent Cookies are called as permanent cookies, which is stored in client hard-drive until it expires . persistent cookies should have set with expiration dates. Sometimes its stays until the user deletes the cookie. Persistent cookies are used to collect identifying information about the user from that system.

Non Persistent Cookies are called as Temporary Cookies. If there is no expires time defined then the cookie is stored in browser memory .

Serverside State Management:
Server side state management means storing the values in the server.

There are two types of server side state management.
1) Application
2) Session

Application:
ASP.NET implements application state using the System.Web.HttpApplicationState class. It provides methods for storing information which can be accessed globally. Information stored on application state will be available for all the users using the website.
E.g Application(“id”) = 1024
Session:
A cookie is very simple and is not suitable for secure storage requirements. Session state is a workaround for this problem and it gives a method to keep more complex objects securely. ASP.NET allows programmers to keep any type of objects in session. Data stored in session will be kept in server memory and it is protected as it will never get transferred to the client

E.g. Session(“userid”)=1024

By using Session.Abandon() we can kill the session.
By using Session.clear() we can clear the content in session
By using Session.Remove(“name”) we can remove the particular session by passing the name.