ViewState: Various ways to reduce performance overhead

Introduction
In this Article, I am going to explore the Viewstate. View state is one thing that always I liked to use. It makes our life easier. As we all know that Viewstate is one way of maintaining the states in the web applications.

As we know, a web page is created every time when a page is posted to the server.It means The values that user entered in the webpage is going to be vanished after the poastback or submit. To get rid of this problem, ASP.NET framework provides a way to maintain these values by virtue of Viewstate. When we enable the viewstate of any control, the value is going to be maintained on every postbacks or server roundtrips.

But how these values get maintained? It doesn’t come free.View state uses hidden valriable that resides on the page to store the controls values. It means that if a page have lots of controls with viewstate enabled the page size would become heavy, in several of kilobytes ie it will take longer time to download.And also on every postback all the data is posted to the server i e increasing the network tarffic as well.

As in new era application, we use lots of heavy controls like gridview etc, on our page which makes page size exponetially heavy.It is always recommended to use viewsate judiciously and even some programmers try to to avoid using this cause of performace overhead.

Here , I am going to discuss how we can reduce the performance overhead caused by viewstate.

Problems with viewstate:
n our new era application, we generally have lots of rich and heavy controls on our page and also provide lots of functionality on the page with the help of few latest technology AJAX etc. To accomplish our task we use Viewstate a lot but as we know it doesn’t come free it has a performance overhead.

As we know viewstate is stored on the page in form of hidden variable. Its always advised to use viewstate as less as possible. we have also other ways to reduce the performance overhead. So we have several ways,here I am going to discuss the following ways

* By compressing decompressing the viewstate
* Aanother way to store the view state at some other server say on web server.

ViewState Compression/Decompression
We can compress the viewstate to reduce the pagesize. By compressing the viewstate we can reduce the viewstate size by more than 30%. But here the question arises, where to compress and decompress the viewstate. For that we have to dig into the Page life cycle. As we know, viewstate is used by the ASP.NET to populate the controls. So we should compress the viewstate after all the changes are done in the viewstate and saving it after compression and we have to decompress the viewstate just before it is used by asp.net to load all the controls from viewstate . So lets jump to thepage life cycle and see our we can fit our requirement.
Page Life Cycle
As we can see there are two methods,One SaveViewState that is responsible for collecting the view state information for all of the controls in its control hierarchy in this method and persist it it in __viewstate hiddenform field. The view state is serialized to the hidden form field in the SavePageStateToPersistenceMedium() method during the save view state stage, and is deserialized by the Page class’s LoadPageStateFromPersistenceMedium() method in the load view state stage. So here in these methods we can compress and decompress the viewstate. Lets take a pictorial view.
Flow
So here, we need to override the methods SavePageStateToPersistenceMedium()for compressing the viewstate and SavePageStateToPersistenceMedium() for decompressing the viewstate. Here I am going to use GZip for compression that is provided by the .NET itself.And this is available in th namespace System.IO.Compression….
Complete article has been published on CodeProject. To view Click here

A walkthrough to Application State

A walkthrough to Application State

As we all know,web is stateless .A Web page is recreated every time it is posted back to the server.In traditional web programming, that all the information within the page and control gets wiped off on every postback.To overcome this problem,ASP.NET framework provides various ways to preserve the states at various stages like controlstate,viewstate,cookies,session etc.These can be defined in at client side and server side state management.Please see the image below.

Various ways to maintain the state
Application LifeCycle
First I am going to explain ASP.NET Application Lifecycle.One need to really understand the application Lifecycle, so that one code efficiently and use the resources available.Also it is very important to discuss, as we are going to Application level events, objects etc.

ASP.NET uses lazy initialization technique for creating the application domains ie Application domain for an application is created only when the first request is recieved by the web server. We can categorise Application life cycle in several stages.These can be

Stage 1: User first requests any resource from the webserver.

Stage 2: Application recieves very first request from the application.

Stage 3: Application basic Objects are created

Stage 4: An HTTPapplication object is assigned to the request.

Stage 5: And the request is processed by the HTTPApplication pipeline

I’ll explain the points one by one.

Stage 1: The Application life cycle starts when a user hits the URL by typing it to on browser.The browser sent this request to the webserver.When webserver recieves the request from browser, it examines the file extension of the requested file and checks which ISAPI extension is required to handle this request and then passes the request to approriate ISAPI extension.
Application state flow

Note 1: If any extension is not mapped to any ISAPI extension, then ASP.NET will not recieve the request and request is handled by the server itself and ASP.NET authentication etc.. will not be applied.

Note 2: We can also make our own custom handler, to process any specific file extension.

Stage 2: When ASP.NET recieves first request, the Application manager creates an application domain for it,Application domain are very important because they provide the isolation amongst various applications on the webserver and every application domain is loaded and unloaded separately and in application domain an instance of class HostingEnvironment is created which provides access to information about all application resources.
Application Lifecycle

Stage 3: After creating the application domain and hosting environment, asp.net initializes the baisc objects as HTTPContext, HTTPRequest and HTTPResponse. HTTPContext holds objects to the specific application request as HTTPRequest and HTTPResponse.HTTPRequest contains all the informaion regarding the current request like cookies, browser information etc. and HTTPResponse contains the response that is sent to client.

Stage 4: Here all the basic objects are being initialized and the application is being started with creation of HTTPApplication class, if there is Global.asax(It is derived from HTTPApplication class) is there in the application then that is instantiated.
Application Life Cycle

Note: When first time the application is accessed the HTTPApplication instance is created for further reqests it may be used other requests as well.

Stage 5
: There are a lot of events are executed by the HTTPApplication class.Here I have listed down few important ones.These events can be used for any specific requirement.

Page Life Cycle

Complete article has been published on CodeProject. To view Click here