View State In ASP.NET

The view state is the state of the page and all its controls. It is automatically maintained across posts by the ASP.NET framework.

When a page is sent back to the client, the changes in the properties of the page and its controls are determined, and stored in the value of a hidden input field named _VIEWSTATE. When the page is again posted back, the _VIEWSTATE field is sent to the server with the HTTP request.

The view state could be enabled or disabled for:

  • The entire application by setting the EnableViewState property in the <pages> section of web.config file.
  • A page by setting the EnableViewState attribute of the Page directive, as <%@ Page Language=”C#” EnableViewState=”false” %>
  • A control by setting the Control.EnableViewState property.

It is implemented using a view state object defined by the StateBag class which defines a collection of view state items. The state bag is a data structure containing attribute value pairs, stored as strings associated with objects.

The StateBag class has the following properties:

Properties Description
Item(name) The value of the view state item with the specified name. This is the default property of the StateBag class.
Count The number of items in the view state collection.
Keys Collection of keys for all the items in the collection.
Values Collection of values for all the items in the collection.

The StateBag class has the following methods:

Methods Description
Add(name, value) Adds an item to the view state collection and existing item is updated.
Clear Removes all the items from the collection.
Equals(Object) Determines whether the specified object is equal to the current object.
Finalize Allows it to free resources and perform other cleanup operations.
GetEnumerator Returns an enumerator that iterates over all the key/value pairs of the StateItem objects stored in the StateBag object.
GetType Gets the type of the current instance.
IsItemDirty Checks a StateItem object stored in the StateBag object to evaluate whether it has been modified.
Remove(name) Removes the specified item.
SetDirty Sets the state of the StateBag object as well as the Dirty property of each of the StateItem objects contained by it.
SetItemDirty Sets the Dirty property for the specified StateItem object in the StateBag object.
ToString Returns a string representing the state bag object.



Managing State In ASP.NET


A new instance of the Web page class is created each time the page is posted to the server. In traditional Web programming, this would typically mean that all information associated with the page and the controls on the page would be lost with each round trip. For example, if a user enters information into a text box, that information would be lost in the round trip from the browser or client device to the server.

To overcome this inherent limitation of traditional Web programming, ASP.NET includes several options that help you preserve data on both a per-page basis and an application-wide basis. These features are as follows:

  • View state
  • Control state
  • Hidden fields
  • Cookies
  • Query strings
  • Application state
  • Session state
  • Profile Properties

View state, control state, hidden fields, cookies, and query strings all involve storing data on the client in various ways. However, application state, session state, and profile properties all store data in memory on the server. Each option has distinct advantages and disadvantages, depending on the scenario.

ASP.NET – Life Cycle

ASP.NET life cycle specifies, how :

  • ASP.NET processes pages to produce dynamic output
  • The application and its pages are instantiated and processed
  • ASP.NET compiles the pages dynamically

The ASP.NET life cycle could be divided into two groups:

  • Application Life Cycle
  • Page Life Cycle

ASP.NET Application Life Cycle

The application life cycle has the following stages:

  • User makes a request for accessing application resource, a page. Browser sends this request to the web server.
  • A unified pipeline receives the first request and the following events take place:
    • An object of the class ApplicationManager is created.
    • An object of the class HostingEnvironment is created to provide information regarding the resources.
    • Top level items in the application are compiled.
  • Response objects are created. The application objects such as HttpContext, HttpRequest and HttpResponse are created and initialized.
  • An instance of the HttpApplication object is created and assigned to the request.
  • The request is processed by the HttpApplication class. Different events are raised by this class for processing the request.


ASP.NET – Calendar Control

Adding a calendar in ASP.NET and displaying it in a webpage is not difficult. One of my friend asked me about the same because he never used this control. So he asked me about sample or demo of Calender Control in ASP.NET.

The Calendar control is a functionally rich web control, which provides the following capabilities

  • Displaying one month at a time
  • Selecting a day, a week or a month
  • Selecting a range of days
  • Moving from month to month
  • Controlling the display of the days programmatically

The basic syntax of a calendar control is :

<asp:Calender ID = "Calendar1" runat = "server">


Lookup Control For ASP.Net Web Applications

Hi friends! here is a new article for you after such a long time. This time I will share with you a Lookup Control that I developed a year ago for my one of web projects. You will find many Lookup controls for web-based and windows-based applications on the internet with various types of functionalities, but most of them are not free. If you want to use them in your application you need to purchase a license and the others that are free to use do not satisfy our requirements. So I decided to design a web user control that will satisfy all my requirements. Of course this is not a professional web user control, it contains some basic functionalities that I want in my application based on requirements. But still it is very useful and it will definitely help those who want to work on such type of user controls and it will also provide you a basic idea about the design and development of web user controls. (more…)