Maintaining state in asp .net.

Web is Stateless means it won’t maintain any data. It cannot store or retrieve the date.

State of controls ie, text of the textbox or the selected item in the drop downlist is not maintained between user request

Each request to a Web server is anonymous and not associated with the previous request. By default, everything on a page is lost when/if you go to another page.

There are many situations where the user enters some data in one page. And in the next page he uses those data which he has entered in the previous page need to be used

For Example ,

We have a registration form, where the user enters his\her details. After entering the details. When he moves to another page, if the user is requested to enter those details once again. User will get offended.
There are several options in asp .net that help us to preserve data on both a per-page basis and an application-wide basis.
Before selecting those options, we need to be clear with our requirements

Is the data that has to be transferred a sensitive one?
How big is the data?
Do u need that data for a single page or for the whole application.
where we want the data to be, client or server.
How long do we want to save the data?



just consider a web application, where we can access our mails by providing our Username and Password.(Gmail).If we are authenticated we can see our mails. A session begins at this instant and ends when user leave the application or a certain amount of time has passed with no site activity. if we close the browser, next time we are directed to login page. The values in the Session object are only applicable to a certain user and may not be accessed by other; nobody else can view our mails. In this session is the most opted one.


Session ("Username") = "Nexgen"



In the Same application, you must have Seen Remember me. If we checked that checkbox, next time when we login, we need not type the username and password. It automatically displays username and password. This is done using cookie.

A good Example from real life

An analogy I like to use is a pay and park. You park your car and get a ticket. When you return with the ticket, you can take your car back. If you don’t have the ticket, then the Ticket Provider won’t be able to tell whether you are there to park your car or take your car back. As such, the ticket is critical to maintaining state between you and the Ticket Provider.

Cookies are used by websites to keep track of visitors. Every time a user visits a website, cookies are retrieved from user machine and help identify the user
Two Types of cookies:

Persistent cookies are stored on your computer hard disk. They stay on your hard disk and can be accessed by web servers until they are deleted or have expired.By default the expiry time is 50 years.It stays there till machine is formatted. Persistent cookies are not affected by your browser setting that deletes temporary files when you close your browser.
Remember me

Non-persistent cookies are saved only while your web browser is running. They can be used by a web server only until you close your browser.


HttpCookie  myCookie = new HttpCookie("NameofCookie");
Response.Cookies["NameofCookie"].Value = "Nexgen";
// Set the cookie expiration date.
myCookie.Expires = DateTime.Now.AddYears(1);
        if (Request.Cookies["NameofCookie"] != null)
            string RetrivalCookie = Request.Cookies["NameofCookie"].Value .ToString ();

An example how to add multiple key/value pairs in single cookie.


 HttpCookie myCookie= new HttpCookie("NameofCookie");
                        myCookie.Values.Add("NameofCookie", txtPassword.Text);
                        DateTime dtExpiry = DateTime.Now.AddMonths(1);                        
 Response.Cookies["NameofCookie"].Expires = dtExpiry;



if we need to maintain the values of DataTable in post back we use view state. The DataTable object that was used to data bind the control initially doesn’t exist anymore once the page is sent to the browser. We need to reproduce the DataTable again. To avoid this we can use the view state .where the DataTable values can be preserved.

When a page is processed, the current state of the page and the controls is hashed into a string and saved in the page as a hidden field. When the page is posted back to the server, the page parses the view state string at page initialization and restores property information in the page.


//******************Saving the table in theViewState*********
            ViewState["chkViewState"] = dtDatatable;

//*******Retrieving the value from  the ViewState*******
     DataTable dt2 = (DataTable)ViewState["chkViewState"];

Query Strings :

Query strings provide a simple but limited way of maintaining some state information.With this option, you can pass values from one page to another as query strings, appended to the end of the page’s url. Use this option if you are not passing any important values like email id, or any other parameter value as it is visible to the user.

The values in the query string passed as key/value pairs separated by an ‘&’ (ampersand).

Happy Coding! 😉


Leave a Reply

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

You are commenting using your 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