Showing posts with label State Management. Show all posts
Showing posts with label State Management. Show all posts

Session in the asp.net

In this article i will explain how session works.

What is session?

A session is specific to the user and for each user a new session is created to track all the request from that user. Every user has a separate session and separate session variable is associated with that session. In case of web applications the default time-out value for session variable is 20 minutes, which can be changed as per the requirement,


Advatantages:

1.It helps to maintain the user states & data all over the application.

2. Any type of object can be stored in session.

3.It is secure.

Disadvantages:

If there are many user at a time on website then performance of the website may get lower as session data will be stored in server memory.


Using Session:

Following code is used to store the variable in session:

Session["variable"] = txtbox1.Text

e.g: Session[“username”] = txtUsername.Text

Like this we can retrieve variable value from the session

Textbox1.text = session[“variable”];


Session ID:

A session ID is an unique identification string usually a long, random and alpha-numeric string, that is transmitted between the client and the server. Session IDs are usually stored in the cookies, URLs (in case url rewriting) and hidden fields of Web pages.

Whenever client makes the request for the data , server looks at the SessionID & retrieves the corresponding data.


Session State:

It is the configuration of the web site for maintaining the session of the user.


Session modes:

It can be configured in web.config file of the website

There are 5 Session modes:

1. Off --> If session mode is off then session will be disabled for whole application.

InProc -->It is the default session mode of the asp.net. If we restart the server then allsession data will be lost in InProc mode.

StateServer --> It is the windows service which runs outside the application. This session state is totally managed by aspnet_state.exe.

In this session data is stored in the sql server.

Custom --> It gives full control to us to create every thing evean a session ID

Session Events:

Session_Start & Session_End are only two events available in asp.net.

Both the events can be handled using globle.asax file.This file has this code for both events.

void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
    }
    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
    }

View state in ASP.NET

View State is ASP.NET

View State is one of the state management technique.
View State is the mechanism that allows state values to be preserved across page postbacks. Because of the stateless nature of the web pages, regular page member variables will not maintain their values after postback.
When we need a page variable to maintain its value across page post backs, we can use ViewState to store that value. Values stored in ViewState will be serialized and sent to the client browser as a value of a hidden form input. When you view the page source (in your browser) of your page you will find that it uses View State, which looks something like this:



This single hidden field contains all the viewstate values for all the page controls.
Because viewstate is (by default) sent to the client browser and then returned to the server in the form of a hidden input control on your page, storing a significant amount of data in viewstate can increase your page size and can affect your page performance.
To disable ViewState for a control, you can set the EnableViewState property to false. When ViewState is disabled for any control, it will also automatically be disabled for all child controls of that control.

Example:



ViewState["vaibhav"] = myValue;
Response.Write(ViewState["vaibhav"]);


Advantages:
A. Easy to store and maintain page level data.
B. Can be set at control level.
C. Encrypted.
Disadvantages:
A. Makes a page heavy, if a good amount of data is stored into it.

What is state Management in asp.net?

WHAT IS STATE MANAGEMENT?
Whenever a page is posted to the server, a new instance of the Web Page’s class is created. This means that all the page/control related information will be lost after the post back. To overcome this obstacle, ASP.net provides several options to help preserve this data.

TYPES OF STATE MANAGEMENT
There are 2 types of State Management approach in ASP.net :
1. Client – Side State Management

1. View State
2. Control State
3. Hidden Fields
4. Cookies
5. Query String


2. Server – Side State Management

1. Application State
2. Session State
3. Profile Properties