Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

A Beginner's Tutorial on ASP.NET State Management

, 17 Feb 2012 CPOL
A Beginner's Tutorial on ASP.NET State Management
StateManagement.zip
StateManagement
App_Data
Global.asax
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)

Share

About the Author

Rahul Rajat Singh
Software Developer (Senior)
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[^]
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 17 Feb 2012
Article Copyright 2012 by Rahul Rajat Singh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid