Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET

A Beginner's Tutorial on ASP.NET State Management

Rate me:
Please Sign up or sign in to vote.
4.77/5 (76 votes)
17 Feb 2012CPOL10 min read 502.3K   3.6K   130  
A Beginner's Tutorial on ASP.NET State Management
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(IsPostBack == true)
        {
            //Store in Viewstate --------------------------------------------------------------
            if (ViewState["number"] != null) //Lets retrieve, increase and store again
            {
                ViewState["number"] = Convert.ToInt32(ViewState["number"]) + 1;
            }
            else //First postback, lets store the info
            {
                ViewState["number"] = 1;
            }

            Label1.Text = ViewState["number"].ToString();

            //Store in Hidden Field -----------------------------------------------------------
            int newVal = Convert.ToInt32(HiddenField1.Value) + 1;
            HiddenField1.Value = newVal.ToString();
            Label2.Text = HiddenField1.Value;


            //Store in Cookies --------------------------------------------------------------
            int postbacks = 0;
            if (Request.Cookies["number"] != null) //Lets retrieve, increase and store again
            {
                postbacks = Convert.ToInt32(Request.Cookies["number"].Value) + 1;
            }
            else //First postback, lets store the info
            {
                postbacks = 1;
            }
            Response.Cookies["number"].Value = postbacks.ToString();

            Label3.Text = Response.Cookies["number"].Value;


            //Store in Application State --------------------------------------------------------------
            Application.Lock();
            Application["number"] = Convert.ToInt32(Application["number"]) + 1;
            Application.UnLock();

            Label5.Text = Application["number"].ToString();

            //Store in Session State --------------------------------------------------------------           
            Session["number"] = Convert.ToInt32(Session["number"]) + 1;

            Label6.Text = Session["number"].ToString();
        }


        //GetDataItem from querystring         
        if (Request.QueryString["number"] != null) //Lets retrieve, increase and store again
        {
            Label4.Text = Request.QueryString["number"];
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //set in query string
        int postbacks = 0;

        if (Request.QueryString["number"] != null) //Lets retrieve, increase and store again
        {
            postbacks = Convert.ToInt32(Request.QueryString["number"]) + 1;
        }
        else //First postback, lets store the info
        {
            postbacks = 1;
        }

        Response.Redirect("default.aspx?number=" + postbacks);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions