Click here to Skip to main content
Click here to Skip to main content

Using Control State in ASP.NET 2.0

, 23 Aug 2006
Rate this:
Please Sign up or sign in to vote.
This article describes a simple approach to maintaining control state in an ASP.NET 2.0 custom web control.

Sample Image - screenshot.jpg

Introduction

This article describes a simple approach to maintaining control state in an ASP.NET 2.0 custom web control. Control state is a new construct within ASP.NET 2.0, and it is really nothing more than view state; however, it is view state with a significant advantage; that advantage is that other developers using your control cannot disable control state as they can view state.

Control state survives even if the developer using the custom control disables view state. The advantage is pretty obvious; in prior versions of Visual Studio, the consumer of a custom control could disable view state; if the control relied on view state to maintain state information, the control would cease to function or at least misbehave. Creating control state removed the ability of a control consumer to disable view state based state management within the control; naturally, the control designer may still opt to not use either view state or control state based state management.

One final note, most of the examples I have personally viewed regarding control state show a mechanization in which only one value is stored in the control state; the accompanying example herein shows how a greater number of values of different data types can be easily stored in the control state.

Getting Started

In order to get started, open up the Visual Studio 2005 IDE, and start a new project. From the New Project dialog (Figure 1), under Project Types, select the “Windows” node from beneath “Visual Basic”, then select the “Web Control Library” template in the right hand pane. Key in a name for the project and then click “OK”.

Once the project has opened, right click on the solution and click on the “Add” menu option, and then select “New Item”. When the “Add New Item” dialog appears (Figure 2), select the “Web Custom Control” template. After selecting the template, key “ExampleStateControl.vb” into the name field, and then click “Add” to close the dialog. You may now delete the default web control that was created when the project was originally initialized from the template.

At this point, we should have an open web control library project, with a single web control named “ExampleStateControl.vb” in that project.

One last step prior to writing the code for this project will be to validate the references and import statements. Compare your import statements with this list, and make any adjustments necessary:

Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Sample Image - 1.jpg

Figure 1: Visual Studio 2005 New Project Dialog

Sample Image - 2.jpg

Figure 2: Add New Item Dialog

We are now ready to add the code necessary to make this control functional. At this point, the project’s code should look something like this:

Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<Serializable()> _
<ToolboxData("<{0}:ExampleStateControl 
    runat="server"></{0}:ExampleStateControl>")> _
Public Class ExampleStateControl
    Inherits WebControl

If we are in sync here, we can now start to write the necessary code. To begin, create a Declarations region and add the following code to that region:

#Region "Declarations"

    Private mCurrentProps As New CurrentProperties

    <Serializable()> _
    Private Structure CurrentProperties
        Dim pFirstName As String
        Dim pLastName As String
        Dim pAge As Integer
        Dim pCity As String
        Dim pState As String
        Dim pCountry As String
        Dim pCitizen As Boolean
    End Structure

#End Region

In examining the declarations, note that there is a structure called CurrentProperties defined, and there is a private member variable declared (mCurrentProps) that is of the CurrentProperties structure type. This instance of the structure is what we will be putting into and collecting out of the control state.

The next thing to do will be to add the properties necessary to support the example custom web control. There is one property for each element contained in the structure. To accomplish this task, create a Properties region and add in the following code:

#Region "Properties"

    <Browsable(True)> _
    <Category("Name")> _
    <DefaultValue("")> _
    <Localizable(True)> _
    <NotifyParentProperty(True)> _
    Public Property FirstName() As String
        Get
            Return mCurrentProps.pFirstName
        End Get
        Set(ByVal value As String)
            mCurrentProps.pFirstName = value
            SaveControlState()
        End Set
    End Property

    <Browsable(True)> _
    <Category("Name")> _
    <DefaultValue("")> _
    <Localizable(True)> _
    <NotifyParentProperty(True)> _
    Public Property LastName() As String
        Get
            Return mCurrentProps.pLastName
        End Get
        Set(ByVal value As String)
            mCurrentProps.pLastName = value
            SaveControlState()
        End Set
    End Property

    <Browsable(True)> _
    <Category("Status")> _
    <DefaultValue("")> _
    <Localizable(True)> _
    <NotifyParentProperty(True)> _
    Public Property Age() As Integer
        Get
            Return mCurrentProps.pAge
        End Get
        Set(ByVal value As Integer)
            mCurrentProps.pAge = value
            SaveControlState()
        End Set
    End Property


    <Browsable(True)> _
    <Category("Status")> _
    <DefaultValue("")> _
    <Localizable(True)> _
    <NotifyParentProperty(True)> _
    Public Property IsCitizen() As Boolean
        Get
            Return mCurrentProps.pCitizen
        End Get
        Set(ByVal value As Boolean)
            mCurrentProps.pCitizen = value
            SaveControlState()
        End Set
    End Property


    <Browsable(True)> _
    <Category("Residence")> _
    <DefaultValue("")> _
    <Localizable(True)> _
    <NotifyParentProperty(True)> _
    Public Property City() As String
        Get
            Return mCurrentProps.pCity
        End Get
        Set(ByVal value As String)
            mCurrentProps.pCity = value
            SaveControlState()
        End Set
    End Property


    <Browsable(True)> _
    <Category("Residence")> _
    <DefaultValue("")> _
    <Localizable(True)> _
    <NotifyParentProperty(True)> _
    Public Property State() As String
        Get
            Return mCurrentProps.pState
        End Get
        Set(ByVal value As String)
            mCurrentProps.pState = value
            SaveControlState()
        End Set
    End Property

    <Browsable(True)> _
    <Category("Residence")> _
    <DefaultValue("")> _
    <Localizable(True)> _
    <NotifyParentProperty(True)> _
    Public Property Country() As String
        Get
            Return mCurrentProps.pCountry
        End Get
        Set(ByVal value As String)
            mCurrentProps.pCountry = value
            SaveControlState()
        End Set
    End Property

#End Region

Note that each property’s get and set methods either update or retrieve from the active instance of the “ControlProps” structure. No other private member variables are used to retain each of the property’s current values.

The next step in the process is to add in the methods used by the control. Add a Methods region to the code page and key in the following methods:

#Region "Methods"

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        Page.RegisterRequiresControlState(Me)
        MyBase.OnInit(e)
    End Sub

    Protected Overrides Function SaveControlState() As Object
        Return Me.mCurrentProps
    End Function

    Protected Overrides Sub LoadControlState(ByVal savedState As Object)
        mCurrentProps = New CurrentProperties
        mCurrentProps = CType(savedState, CurrentProperties)
    End Sub

#End Region

Notice that in the OnInit, the first line is used to instruct the page to maintain control state. The next two methods are used to save the current control state or to reload the control state's values from the saved control state.

At this point, the only thing left to do is to define how the control will be rendered. To complete this step, create a “Rendering” region and, within this region, override the RenderContents method with the following code:

#Region "Rendering"

    Protected Overrides Sub RenderContents(ByVal writer As _
                            System.Web.UI.HtmlTextWriter)

        Try
            writer.RenderBeginTag(HtmlTextWriterTag.Table)
            writer.Write("<b>Control State Properties</b><hr />")
            '''''''''''''''''''''''''''''''''''''''''''''
            writer.RenderBeginTag(HtmlTextWriterTag.Tr)

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write("Name: ")
            writer.RenderEndTag()

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write(mCurrentProps.pFirstName + " " + _
                         mCurrentProps.pLastName)
            writer.RenderEndTag()

            ''''''''''''''''''''''''''''''''''''''''''''
            writer.RenderBeginTag(HtmlTextWriterTag.Tr)

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write("Age: ")
            writer.RenderEndTag()

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write(mCurrentProps.pAge)
            writer.RenderEndTag()

            writer.RenderEndTag()

            ''''''''''''''''''''''''''''''''''''''''''''
            writer.RenderBeginTag(HtmlTextWriterTag.Tr)

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write("Is Citizen:     ")
            writer.RenderEndTag()

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write(mCurrentProps.pCitizen.ToString())
            writer.RenderEndTag()

            writer.RenderEndTag()

            ''''''''''''''''''''''''''''''''''''''''''''
            writer.RenderBeginTag(HtmlTextWriterTag.Tr)

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write("City: ")
            writer.RenderEndTag()

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write(mCurrentProps.pCity.ToString())
            writer.RenderEndTag()

            writer.RenderEndTag()

            ''''''''''''''''''''''''''''''''''''''''''''
            writer.RenderBeginTag(HtmlTextWriterTag.Tr)

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write("State: ")
            writer.RenderEndTag()

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write(mCurrentProps.pState.ToString())
            writer.RenderEndTag()

            writer.RenderEndTag()

            ''''''''''''''''''''''''''''''''''''''''''''
            writer.RenderBeginTag(HtmlTextWriterTag.Tr)

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write("Country: ")
            writer.RenderEndTag()

            writer.RenderBeginTag(HtmlTextWriterTag.Td)
            writer.Write(mCurrentProps.pCountry.ToString())
            writer.RenderEndTag()

            writer.RenderEndTag()

            ''''''''''''''''''''''''''''''''''''''''''''

            writer.RenderEndTag() ' close the table

        Catch ex As Exception

            writer.Write(Me.ID)

        End Try

    End Sub

#End Region

As you can see, the rendering of this example control is pretty simple; it is just generating a table, and displaying each control state maintained property value in separate rows within that table.

The control is now complete. Prior to testing the control, rebuild the project. Once that has been completed and any errors encountered are repaired, it is time to test the control. To test the control, add a new web site project to the web control library project currently open. Once the test web site has been created, set the test project as the start up project by right clicking on the web site solution in the Solution Explorer and selecting the “Set as Start Up Project” menu option. Next, locate the Default.aspx page in the web site solution, right click on this page, and select the “Set as Start Page” menu option.

If you have downloaded the sample project, you may prefer to open IIS and create a virtual directory pointing to the web site and then open the project from within Visual Studio 2005. The solution contains both the sample web site and the sample custom web control library project.

Open the Default.aspx page for editing. Locate the newly created control in the toolbox (it should be at the top) and, if you are not working with the sample web site, drag the “ExampleStateControl” control onto the page (Figure 3).

Sample Image - 3.jpg

Figure 3: Custom Control in the Toolbox

If you are working with the sample code, you may still click on the control and set its properties through the property grid within the Visual Studio 2005 IDE, if you wish to do so.

Build the application and run it; you should now be looking at the site displayed in the following figure (Figure 4). With the page running, you may key in new values into the spaces provided in the lower half of the page and hit the Enter key or click on the Submit button to force a post back. In either case, the application will update the current properties and save the control state; after the post back has finished, you will note that the values stored in the control state have been used in the rendering of the control.

Sample Image - 4.jpg

Figure 4: Control State Managed Control in Operation

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

salysle
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
GeneralGood Article PinmemberMember 712799415-Sep-10 23:52 
Generalhorrible idea, do not abuse control state PinmemberD. Jin11-Jun-07 10:44 
QuestionHandling ReadOnly Properties? PinmemberShutlOrbit2-Dec-06 19:58 
Thank you for your very helpful article!
There is no Set function on ReadOnly properties. The ones I'm working with reflect the result of internal processing and the user is not allowed to change the property. I imagine calling SaveControlState() at the end of each method in the class would cover everything. Is there a better way to handle ReadOnly properties?
Regards,
Rick
 
Matter is mostly space.

AnswerRe: Handling ReadOnly Properties? Pinmembersalysle3-Dec-06 5:51 
GeneralExcellent Article PinmemberRussell Aboobacker3-Sep-06 22:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 23 Aug 2006
Article Copyright 2006 by salysle
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid