Content

  • ASP.NET State Management
  • Viewstate
  • Session State
  • Application State
  • Differencae between Session & Viewstate

ASP.NET State Management

A new instance of the Web page class is created each time the page is posted to the server. In traditional Web programming, this would typically mean that all information associated with the page and the controls on the page would be lost with each round trip. For example, if a user enters information into a text box, that information would be lost in the round trip from the browser or client device to the server.

To overcome this inherent limitation of traditional Web programming, ASP.NET includes several options that help you preserve data on both a per-page basis and an application-wide basis.


Viewstate

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.

Example :
Frond-End code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>ViewState Example</h3>
    <asp:TextBox runat="server" ID="txtcount"></asp:TextBox>
    <asp:Button runat="server" Text="Click" ID="btnclick" OnClick="btnclick_Click"  />
    </div>
    </form>
</body>
</html>
Back-End code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
    
    namespace Training
    {
        public partial class ViewState2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (ViewState["Clicks"] == null)
                    {
                        ViewState["Clicks"] = 0;
                    }
                    txtcount.Text = ViewState["Clicks"].ToString();
                }
            }
    
            protected void btnclick_Click(object sender, EventArgs e)
            {
                int count = (int)ViewState["Clicks"] + 1;
                txtcount.Text = count.ToString();
                ViewState["Clicks"] = count;
            }
        }
}

Session State

Session State is another state management technique to store state, meaning it helps in storing and using values from previous requests. Whenever the user requests a web form from a web application it will get treated as a new request. an ASP.NET session will be used to store the previous requests for a specified time period.

Front-End Code
<html xmlns="http://www.w3.org/1999/xhtml">
            <head runat="server">
                <title></title>
            </head>
            <body>
                <form id="form1" runat="server">
                <div>
                    <h3>Session Example</h3>
                <asp:TextBox> runat="server" ID="txtcount"></asp:TextBox>
                <asp:Button runat="server" Text="Click" ID="btnclick" OnClick="btnclick_Click"  />
                </div>
                </form>
            </body>
            </html>
            
Back-End code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
                
                namespace Training
                {
                    public partial class Session1 : System.Web.UI.Page
                    {
                        protected void Page_Load(object sender, EventArgs e)
                        {
                            if (!IsPostBack)
                            {
                                if (Session["Clicks"] == null)
                                {
                                    Session["Clicks"] = 0;
                                }
                                txtcount.Text = Session["Clicks"].ToString();
                            }
                        }
                
                        protected void btnclick_Click(object sender, EventArgs e)
                        {
                            int count = (int)Session["Clicks"] + 1;
                            txtcount.Text = count.ToString();
                            Session["Clicks"] = count;
                        }
                    }
                }

You can access count value in another web pages also


Application State

  • Application state is used to maintain the state on the server side. Its state is available through out all the users of the application. It means these state values are maintained on the application level and not on the user level and user session level.
  • Application state does not have default expiry or you can not set an expiry of the application state.
  • Application State variables are like multi-user Global data.
  • Application state is a data repository available to all classes in an ASP.NET application.
  • Unlike session state, which is specific to a single user session, application state applies to all users and sessions.
  • Application state is a useful place to store small amounts of often-used data that does not change from one user to another.

Differencae between Session & Viewstate

  • ViewState stores data on single page
  • ViewState is client side state management technique
  • Session stores data on whole website pages
  • Session is a server side state management technique