Tuesday, August 9, 2011

Understanding Postbacks

Hi newbies, in this session I try to explain about the term called "POSTBACK" in as unsophisticated and effortless way as I possibly can. In very simple terms, a postback means one single round trip to the server, i.e. a call(request) made to the server by a client (browser) and its corresponding response by the server, together is known as a postback.
          In ASP.NET, a postback can be identified by a page refresh, which means, a good page should have minimum number of postbacks- I mean, who likes to see page refreshes, until unless, its done intentionally, right??? To send some data, and then receive the response back, there is no alternate way but to do a postback. But nothing much to worry, since there already are ways, to hide such page refreshes (using AJAX calls, will talk about later). So, for now, one of the major thing to keep in mind is, to minimize the number of postbacks. It is one very major trick in developing a good, efficient and intuitive webpage using ASP.NET.
          Always attached with postbacks, is another very closely interlinked term "VIEWSTATE" . One thing to always keep in mind during web developemnt is that, HTTP is stateless at its core. HTTP (HyperText Transmission Protocol) is the predominant technology used to communicate on internet. Details are out of our topic of concern. Stateless, means, everytime a page loads, even though you see everything similar to the one before postback, it is not the same as the previous ones. The controls are regenerated and entirely new ones, which have no assoication or relation or link to the older ones. That's the innate characteristics of the HTTP, which has its own positives and negatives. So, even though we like it or not, we are bound to cope with it (but trust me, it's not as bad as it might sound in the first time). Thus, coming back to our discussion of VIEWSTATE, new controls implies controls with no data or default data. This is one major concern on the web. The users are not supposed to know about this stateless feature of the HTTP; and its the responsibility of the developers to hide it from the users. Imagine, you fill a long-long sign up form, click submit, and due to some error, the page returns back to the same page, with all the values inside the text-boxes gone. How hectic it would be, to refill the same form again-and-again. Yes, this is where, VIEWSTATE comes to our rescue. Viewstate is the ASP.NET's answer to HTTP-statelessness.  Here is an excerpt from the msdn site :

"View state is the method that the ASP.NET page framework uses to preserve page and control values between round trips. When the HTML markup for the page is rendered, the current state of the page and values that must be retained during postback are serialized into base64-encoded strings. This information is then put into the view state hidden field or fields."

         It is an automatic process that occurs with every postback, and the handler library (aspnet_isapi.dll) internally handles it. As discussed in my previous section about the page-events, viewdata maintenance is handled in between the Page.PreRender and Page.Render events. Every time a page is reloaded, the previous data is restored back to the controls just before it is re-server by the server. Of course, it is a matter of choice, and can be opted not to be restored according to the demand of the situation. The generated viewstate is encrypted for obvious reasons. The encrypted version of the viewstate stored inside the webpage can be viewed from its page source. It is stored inside the hidden form field, "__VIEWSTATE" variable . A sample code to maintain a page's viewstate using the built-in classes of the .NET platform is shown below :

/* CODE BLOCK */


       Thank you all for now. Next time, we'll get into the code and create our first ASP.NET webpage. Take care till then. Live life.