Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

ControlState Property Demystified

ASP.NET web applications are based on stateless HTTP protocol. New instance of the web page class is created each time the page is requested from the server.

 

So, this would mean that all data on the page will be lost when page is posted to server. Then how do we create pages with controls which can preserve data across page postbacks.

To overcome this inherent limitation of Web programming, the ASP.NET page framework offers several state-management features, one of which is ViewState, to preserve page and control values between round trips to the web server. You can access view state using page's ViewState property to preserve data during round trips to the web server. The ViewState property maintains ViewState information using key/value pairs

But ViewState has one major drawback. ViewState property can be disabled. In ASP.NET 1.x, Custom controls had only option of using ViewState to store critical information across postbacks. But a developer using custom controls can disable ViewState which can ultimately break the control.

To fix this, ASP.NET 2.0 has introduced a new kind of ViewState called ControlState which is essentially a private ViewState for your control only, and it is not affected when ViewState is turned off. You should only store data in the ControlState collection that is absolutely critical to the functioning of the control.

Use ControlState to store small amounts of critical information. Heavy usage of ControlState can impact the performance of application because it involves serialization and deserialization for its functioning.

This article describes the implementation of control state in an ASP.NET 2.0 web custom control. To see things getting started, download example ControlState Visual Studio .Net project, used in this tutorial.


Project Templates

Example will show how to create custom control name ApartmentListing that uses ControlState property to preserves state across page requests. As told earlier, ControlState cannot be disabled so that critical information is available on postbacks for the control to function properly.

Implementation of ApartmentListing custom control illustrates three important tasks that you must perform to enable a control to participate in a control state:

  • Override the OnInit method and invoke the RegisterRequiresControlState method to register with the page for participation in control state. This must be done with each request.

  • Override the SaveControlState method to save data in control state.

  • Override the LoadControlState method to load data from control state. This method calls the base class method and gets the base class's contribution to control state.

The ApartmentListing control renders HTML table element which holds properties by overriding the inherited RenderContents method. The parameter passed into the RenderContents method is an object of type HtmlTextWriter, which is a utility class that has methods for rendering tags and other HTML (and HTML-variant) markup. This improves performance because the HtmlTextWriter object writes directly to the output stream

At the class level, ApartmentListing is marked with the following attributes:

  • DefaultPropertyAttribute is a design-time attribute that specifies the default property of a control. In design mode, the properties dialog box typically highlights the default property when a page developer clicks the control on the design surface.

  • ToolboxDataAttribute specifies the format string for the element. The string becomes the control's markup when the control is double-clicked in the toolbox or dragged from the toolbox onto the design surface. For ApartmentListing, the string creates this element:

<cc1:ApartmentListing ID="ApartmentListing1" runat="server" Apartment="1BHK" LeasePeriod="1 yr" Rent="$1000" Space="1000sq" State="IL" />

The following attributes are applied to public properties of ApartmentListing which are standard design-time attributes that you will generally apply to all public properties of your controls:

  • BindableAttribute, specified as true or false, specifies for visual designers whether it is meaningful to bind the property to data. For example, in Visual Studio 2005, if a property is marked with Bindable(true), the property is displayed in the DataBindings dialog box. If a property is not marked with this attribute, the property browser infers the value to be Bindable(false).

  • CategoryAttribute specifies how to categorize the property in the properties dialog box. For example, Category("Appearance") tells the property browser to display the property in the Appearance category when the page developer uses the category view of the properties dialog box. You can specify a string argument corresponding to an existing category in the property browser or create your own category.

  • DescriptionAttribute gives a brief description of the property. Usually, the property browser displays the description of the selected property at the bottom of the Properties window.

  • DefaultValueAttribute specifies a default value for the property.

  • LocalizableAttribute, specified as true or false, when a property is marked Localizable (true), property will included when serializing localized resources. The property value will be persisted to localization source when the control is polled for localizable properties.

We are using structure variable to retrieve and set the properties values for ApartmentListing custom control using the defined properties for each structure element.

To test the ApartmentListing custom control, rebuild the project. Now add new website to existing solution that hosts ApartmentListing web control library project. After adding new website, you can find ApartmentListing custom control in Toolbox. Drag and drop the ApartmentListing control onto the page as shown below

<%@ Register Assembly="WebControlLibrary1" Namespace="WebControlLibrary1" TagPrefix="cc1" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <cc1:ApartmentListing ID="ApartmentListing1" runat="server" />

Now, you can deal with ApartmentListing custom control in the same manner as you do with a standard ASP.NET control. For example, to set the values for properties for ApartmentListing control, right click the control in design mode and set the properties as shown below:


Properties Dialog box

Now, Build the solution by pressing F5 button. You can see the output as shown below which displays the values for defined properties for ApartmentListing control.


ApartmentListing Custom Control

It's time to see the magic of ControlState property. I change the value for ApartmentType property value by entering a new value in textbox and click submit button as shown below. You can see that when we click submit button, it generates a postback. After page postback, you can see that ApartmentListing control has persisted properties values across page postback. It also shows the updated value for ApartmentType property.

Now you can go ahead and create your custom controls which can persist some critical data across page requests.


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   Google