Difference between revisions of "State Management in ASP.NET"

From JholJhapata
 
(5 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
== Client-side | Techniques ==
 
== Client-side | Techniques ==
 
=== View State ===
 
=== View State ===
View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.Please keep in mind though, that while cookies and sessions can be accessed from all your pages on your website, ViewState values are not carried between pages.<br><br>
+
[[ASP.NET View State|View State]] is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.Please keep in mind though, that while cookies and sessions can be accessed from all your pages on your website, ViewState values are not carried between pages.
 
 
'''Points of ViewState:'''
 
* ViewState does not hold the controls, rather it holds the values of the form controls and their corresponding ID's that would otherwise be lost due to a post back because they do not post with the form. 
 
* ViewState is not used to hold session data or to transmit data between pages.
 
* ViewState does not recreate the dynamically created controls of a page.
 
* It does not restore the values to the controls after a post back operation.
 
* ViewState for a control is disabled, still the value would be retained after a post back of the page occurs, for input controls like TextBox or DropDownList.
 
 
 
So, ViewState represents the state of a page when it was last processed on the web server.  It holds the values of a control that has been dynamically changed.
 
 
 
'''How does ViewState work?'''
 
 
 
When the page is first created all controls are serialized to the ViewState, which is rendered as a hidden form field named __ViewState.  This hidden field corresponds to the server side object known as the ViewState.  ViewState for a page is stored as key-value pairs using the System.Web.UI.StateBag object.  When a post back occurs, the page de-serializes the ViewState and recreates all controls.  The ViewState for the controls in a page is stored as base 64 encoded strings in name - value pairs.  When a page is reloaded two methods pertaining to ViewState are called, namely the LoadViewState method and SaveViewState method.  The following is the content of the __ViewState hidden field as generated for a page in my system.
 
 
 
[[File:ViewState.png|ViewState]]
 
 
 
'''ViewState can be enabled and disabled in any of the following ways:'''
 
 
 
* Control Level
 
* Page Level
 
* Application Level
 
* Machine Level
 
 
   
 
   
 
=== Hidden field ===
 
=== Hidden field ===
 +
[[ASP.NET Hidden Field|Hidden Field]] is non visual control in ASP.NET where we can save the value. This is one of the types of client-side state management tools. It stores the value between the roundtrip. Anyone can see Hidden Field details by simply viewing the source of document.
 
=== Cookies ===
 
=== Cookies ===
 +
[[ASP.NET Cookies|Cookies]] is a small piece of information stored on the client machine. Its is used to store user preference information like Password, City, SessionId etc on client machines. We need to import namespace called Systen.Web before we use cookie(HttpCookie).
 
=== Control State ===
 
=== Control State ===
 +
The '''control state''' is a way to save a control’s state information when the EnableViewState property is turned off. Unlike ViewState a developer can’t turn off control state. The control state can be implemented only in controls that you implement. ControlState is used to store small amounts of critical information. Heavy usage of ControlState can impact the performance of application because it involves '''serialization''' and '''deserialization''' for its functioning.
 +
 +
There are two methods('''SaveControlState''' and '''LoadControlState''') which we have to implement in your custom control and on '''OnInit''' method of the control we need to add the call for the '''Page.RegisterRequiresControlState()''' method with the instance of the control to register.
 +
 +
    public class ControlWebControl : Control
 +
    {
 +
        private string _strStateToSave;
 +
        protected override void OnInit(EventArgs e)
 +
        {
 +
            Page.RegisterRequiresControlState(this);
 +
            base.OnInit(e);
 +
        }
 +
        protected override object SaveControlState()
 +
        {
 +
            return _strStateToSave;
 +
        }
 +
        protected override void LoadControlState(object state)
 +
        {
 +
            if (state != null)
 +
            {
 +
                _strStateToSave = state.ToString();
 +
            }
 +
        }
 +
    }
 +
 
=== Query Strings ===
 
=== Query Strings ===
 +
[[ASP.NET Query Strings|Query string]] is one of the techniques in Web applications to send data from one page to another. A query string consists of two parts, field and value, and each of pair separated by ampersand (&). The ?(question mark in a query string indicates the beginning of a query string and it's value.
 +
 
== Server-side | Technique ==
 
== Server-side | Technique ==
 
=== Session State ===
 
=== Session State ===
 
==== InProc ====
 
==== InProc ====
 
==== OutProc ====
 
==== OutProc ====
 +
ASP.NET session state is designed to enable you to easily store user session data in different sources for your ASP.NET applications. By default, session state values and information are stored in memory within the ASP.NET process. Alternatively, you can store [[OutProc Session SQLServer|session data in a SQL Server database]], where it can be shared by multiple Web servers. For more information about session state, see Implementing a Session-State Store Provider and Session-State Modes.
 +
 
=== Application State ===
 
=== Application State ===

Latest revision as of 12:21, 3 August 2022

As we all know, HTTP or HTTPS is stateless protocol. It just cleans up or we can say removes all the resources/references that were serving a specific request in the past.

Stateless means, whenever we visit a website, our browser communicates with the respective server depending on our requested functionality or the request. In this case HTTP/HTTPs doesn't remember what website or URL we visited, or in other words we can say it doesn't hold the state of a previous website that we visited before closing our browser, that is called stateless.

So our browsers are stateless.

State Management Types

State Management Techniques

Client-side | Techniques

View State

View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.Please keep in mind though, that while cookies and sessions can be accessed from all your pages on your website, ViewState values are not carried between pages.

Hidden field

Hidden Field is non visual control in ASP.NET where we can save the value. This is one of the types of client-side state management tools. It stores the value between the roundtrip. Anyone can see Hidden Field details by simply viewing the source of document.

Cookies

Cookies is a small piece of information stored on the client machine. Its is used to store user preference information like Password, City, SessionId etc on client machines. We need to import namespace called Systen.Web before we use cookie(HttpCookie).

Control State

The control state is a way to save a control’s state information when the EnableViewState property is turned off. Unlike ViewState a developer can’t turn off control state. The control state can be implemented only in controls that you implement. ControlState is used to store small amounts of critical information. Heavy usage of ControlState can impact the performance of application because it involves serialization and deserialization for its functioning.

There are two methods(SaveControlState and LoadControlState) which we have to implement in your custom control and on OnInit method of the control we need to add the call for the Page.RegisterRequiresControlState() method with the instance of the control to register.

   public class ControlWebControl : Control
   {
       private string _strStateToSave;
       protected override void OnInit(EventArgs e)
       {
           Page.RegisterRequiresControlState(this);
           base.OnInit(e);
       }
       protected override object SaveControlState()
       {
           return _strStateToSave;
       }
       protected override void LoadControlState(object state)
       {
           if (state != null)
           {
               _strStateToSave = state.ToString();
           }
       }
   }

Query Strings

Query string is one of the techniques in Web applications to send data from one page to another. A query string consists of two parts, field and value, and each of pair separated by ampersand (&). The ?(question mark in a query string indicates the beginning of a query string and it's value.

Server-side | Technique

Session State

InProc

OutProc

ASP.NET session state is designed to enable you to easily store user session data in different sources for your ASP.NET applications. By default, session state values and information are stored in memory within the ASP.NET process. Alternatively, you can store session data in a SQL Server database, where it can be shared by multiple Web servers. For more information about session state, see Implementing a Session-State Store Provider and Session-State Modes.

Application State