State Management in ASP.Net (Part I)

Sometimes and almost every web application need to track the user state, or data that  relevant on the application it self. for example, log on information. The aplication remember the user information such as its roles in that aplication, history, and what they are working on in that aplication.

In ASP.Net there are two approach to handle this, by using Client Side State Management or Server Side State Management.

1. Client Side State Management.
Client side state management stores the information on the client's computer  machine  by embeding the information into a web page, URL,  Cookies,  or Cache.
some of client side state management are:

1.a View State.
ASP.net uses the view state to store your information on a page request. You will not loose your information after the page postback. But when you move to another page, your information will be deleted.

example of using View State:

public partial class ViewStateDemo1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ViewState["clicks"] != null)
            {
                ViewState["clicks"] = (int)ViewState["clicks"] + 1;
            }
            else
            {
                ViewState["clicks"] = 1;
            }
            Label1.Text = "ViewState[click] = " + ((int)ViewState["clicks"]).ToString();
        }
    }

this view state save information on how many time user click a button to do a past request on the page it self. the value of ViewState["clicks"] will always increment as long as the user clicks a button on that page. but if user move to another page, the value will be reset to null again.

1.b Hidden Fields
the hidden fields is an asp.net control that store the information without displaying it to users.

example using hidden fields:
public partial class HiddenFieldDemo1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int clicks;
            int.TryParse(HiddenField1.Value, out clicks);
            clicks++;
            HiddenField1.Value = clicks.ToString();
            Label1.Text = "Hidden Field Value = " + HiddenField1.Value;
        }
    }

1.c Cookies
Cookies store a value  in user's browser that the browser sends with every page request to the same server. Cookies are the best way to store an information that required on multiple web pages. because NOT like view state or hidden fields, the data that store in the cookies will never be reset, although you redirect to another pages, or you close your browser.

example using Cookies:

public partial class CookiesDemo1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //read and increment the cookie
            int cookiesClicks;
            if (Request.Cookies["clicks"] != null)
            {
                cookiesClicks = int.Parse(Request.Cookies["clicks"].Value) + 1;
            }
            else
                cookiesClicks = 1;

            //save the cookies on the next visit to this page
            Response.Cookies["clicks"].Value = cookiesClicks.ToString();

            Label1.Text = "Cookie Value: " + cookiesClicks.ToString();
        }
    }

the value of cookiesClicks will always increment and never be reset. (except you clear the cookies from your browser it self)


For the Server side state management i'll describe on the next posting
Next Recommended Reading State Management in ASP.NET