A day with .Net

My day to day experince in .net

State management in ASP.NET Part-2

Posted by vivekcek on July 29, 2009

2. Hidden Field
Hidden fields are just input fields that are embedded in HTML tag that is not visible to user. Hidden fields stores information of a single page like view state.
3. Cookies
A cookie is a small amount of data stored in client side, and passed with all page requests. There are two types of cookies persistent and temporary. Persistent cookies are stored in client side as a text file, and these cookies will not be destroyed even if the user shut down the system. So these cookies are used in a later time. But the temporary cookies are stored in browsers memory, which will be active for the current session only. For using cookies you have to add the System.Net name space.
An example code to implement Remember me feature in your login

Using System.Net;  

protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            HttpCookie cookie = Request.Cookies["mycookie"];

            if (cookie != null)
                string s = cookie["uname"].ToString();
                if (s == "vivek")
    protected void Button1_Click(object sender, EventArgs e)
        HttpCookie cookie = new HttpCookie("mycookie");
        cookie["uname"] = txtCookie.Text;
        cookie.Expires = DateTime.Now.AddYears(1);

In the above code

cookie.Expires = DateTime.Now.AddYears(1);

Specifies the scope of cookie,Another properties avilable for setting cookie scope are.


4.Query String
Adding query string to URL from page1.aspx

Response.Redirect(page2.aspx?qid=" + 123 + "&tId=" + 34);

Acessing the query strings in page2.aspx

 string key = Request.QueryString["qid"];
 string _Tid = Request.QueryString["tId"]; ;

4. Application State
The ASP.NET application variables are not user specific. Application variables are initialized when the application started and destroys when server is restarted or IIS restarted. You can declare an application variable at the Application Start method in Global.asax file. Application variables are normally used to count application hits and finding number of users online.

protected void Page_Load(object sender, EventArgs e)
        int count = 0;
        if (Application["hit"].ToString() != string.Empty)
            count = Convert.ToInt32(Application["hit"]);
            Application["hit"] = count;

 void Application_Start(object sender, EventArgs e) 
        Application["hit"] = "";


5.Session State

Session variables are user specific.Applicable to a single session,They are also declared in Global.asax as.

void Session_Start(object sender, EventArgs e) 
        Session["uname"] = "";


Every client that accesses the application has a different session and a distinct collection of information.it forces the web server to store additional information in memory. This extra memory requirement,even if it is small, can quickly grow to performance-destroying levels as thousands of clients access the site.

ASP.NET tracks each session using a unique 120-bit identifier


One Response to “State management in ASP.NET Part-2”

  1. Nice Post dude…. Keep going..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s