Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » ASP.NET & Web Forms » Maintain Control State in ASP.NET 2.0

Maintain Control State in ASP.NET 2.0

This article describes a simple approach to maintaining control state in an ASP.NET 2.0 custom web control.

Author Rank :
Page Views : 14343
Downloads : 253
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ControlStateExample.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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 store 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 "C#", 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.cs" 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.cs" 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:

using System;

using System.ComponentModel;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

ControlState1.gif

Figure 1:  Visual Studio 2005 New Project Dialog

ControlState2.gif

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:

using System;

using System.ComponentModel;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


[Serializable() ]
[ToolboxData("<{0}:ExampleStateControl runat=server></{0}:ExampleStateControl>")]

public class ExampleStateControl : 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 CurrentProperties mCurrentProps = new CurrentProperties();

                  

[Serializable()]private struct CurrentProperties

{

    public string pFirstName;

    public string pLastName;

    public int pAge;

    public string pCity;

    public string pState;

    public string pCountry;

    public bool pCitizen;

}

                  

#endregion

 

In examing 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 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 string FirstName

{

    get

    {

        return mCurrentProps.pFirstName;

    }

    set

    {

        mCurrentProps.pFirstName = value;

        SaveControlState();

    }

}                  

                  

[Browsable(true)]

[Category("Name")]

[DefaultValue("")]

[Localizable(true)]

[NotifyParentProperty(true)]

public string LastName

{

    get

    {

        return mCurrentProps.pLastName;

    }

    set

    {

        mCurrentProps.pLastName = value;

        SaveControlState();

    }

}                  

                  

[Browsable(true)]

[Category("Status")]

[DefaultValue("")]

[Localizable(true)]

[NotifyParentProperty(true)]

public int Age

{

    get

    {

        return mCurrentProps.pAge;

    }

    set

    {

        mCurrentProps.pAge = value;

        SaveControlState();

    }

}          


[Browsable(true)]

[Category("Status")]

[DefaultValue("")]

[Localizable(true)]

[NotifyParentProperty(true)]

public bool IsCitizen

{

    get

    {

        return mCurrentProps.pCitizen;

    }

    set

    {

        mCurrentProps.pCitizen = value;

        SaveControlState();

    }

}                  

 

[Browsable(true)]

[Category("Residence")]

[DefaultValue("")]

[Localizable(true)]

[NotifyParentProperty(true)]

public string City

{

    get

    {

        return mCurrentProps.pCity;

    }

    set

    {

        mCurrentProps.pCity = value;

        SaveControlState();

    }

}

               

[Browsable(true)]

[Category("Residence")]

[DefaultValue("")]

[Localizable(true)]

[NotifyParentProperty(true)]

public string State

{

    get

    {

        return mCurrentProps.pState;

    }

    set

    {

        mCurrentProps.pState = value;

        SaveControlState();

    }

}

                

[Browsable(true)]

[Category("Residence")]

[DefaultValue("")]

[Localizable(true)]

[NotifyParentProperty(true)]

public string Country

{

    get

    {

        return mCurrentProps.pCountry;

    }

    set

    {

        mCurrentProps.pCountry = value;

        SaveControlState();

    }

}

                  

#endregion

Note that each property's get and set methods either updates or retrieves 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 override void OnInit(System.EventArgs e)

{

    Page.RegisterRequiresControlState(this);

    base.OnInit(e);

}                  

                  

protected override object SaveControlState()

{

    return this.mCurrentProps;

}                  

                  

protected override void LoadControlState(object savedState)

{

    mCurrentProps = new CurrentProperties();

    mCurrentProps = (CurrentProperties) savedState;

}

                  

#endregion

Notice that in the init, 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 states 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 sub with the following code:

#region "Rendering"

                  

protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)

{                            

    try

    {

        writer.RenderBeginTag(HtmlTextWriterTag.Table);

        writer.Write("<b>Control State Properties</b><hr />");

        ///''''''''''''''''''''''''''''''''''''''''''

        writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                                     

        // first column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write("Name: ");

        writer.RenderEndTag();

                                     

        // second column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write(mCurrentProps.pFirstName + " " + mCurrentProps.pLastName);

        writer.RenderEndTag();

                                     

        ///'''''''''''''''''''''''''''''''''''''''''

        writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                                     

        // first column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write("Age: ");

        writer.RenderEndTag();

                                     

        // second column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write(mCurrentProps.pAge);

        writer.RenderEndTag();                                     

        writer.RenderEndTag();

                                     

        ///'''''''''''''''''''''''''''''''''''''''''

        writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                                     

        // first column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write("Is Citizen:     ");

        writer.RenderEndTag();

                                     

        // second column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write(mCurrentProps.pCitizen.ToString());

        writer.RenderEndTag();                                     

        writer.RenderEndTag();

                                     

        ///'''''''''''''''''''''''''''''''''''''''''

        writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                                     

        // first column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write("City: ");

        writer.RenderEndTag();

                                     

        // second column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write(mCurrentProps.pCity.ToString());

        writer.RenderEndTag();

                                     

        writer.RenderEndTag();

                                           

        ///'''''''''''''''''''''''''''''''''''''''''

        writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                                     

        // first column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write("State: ");

        writer.RenderEndTag();

                                     

        // second column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write(mCurrentProps.pState.ToString());

        writer.RenderEndTag();

                                     

        writer.RenderEndTag();                                     

                                     

        ///'''''''''''''''''''''''''''''''''''''''''

        writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                                     

        // first column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write("Country: ");

        writer.RenderEndTag();

                                     

        // second column

        writer.RenderBeginTag(HtmlTextWriterTag.Td);

        writer.Write(mCurrentProps.pCountry.ToString());

        writer.RenderEndTag();

                                     

        writer.RenderEndTag();

                                     

        ///'''''''''''''''''''''''''''''''''''''''''                                     

        writer.RenderEndTag(); // close table

    }

    catch (Exception)

    {                                     

        writer.Write(this.ID);                                     

    }                            

}                

                  

#endregion

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 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).

ControlState3.gif

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.

ControlState4.gif

Figure 4:  Control state managed control in operation

NOTE: THIS ARTICLE IS CONVERTED FROM VB.NET TO C# USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON VB.NET Heaven (http://www.vbdotnetheaven.com/). 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Scott Lysle
Freelance software developer residing in Alabama. Bachelors, Masters Degrees from Wichita State University. I spent the first half of my career working on aircraft controls and displays and in that time I worked on the cockpits for the OH-58 AHIP, the AH-1W, the V-22, the F-22, the C-130J, the C-5 AMP, AWACS, JPATS, and a few others. Since 1997 I have been largely involved with Windows and web development, GIS application development, consumer electronics development (embedded linux/java), but still sometimes work on aircraft and military projects, the most recent of which was the presidential transport helicopter. I tend to work primarily with C/C++, Java, VB, and C#.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
thanku by Poonam On July 2, 2009
This is excellent example of control state
thanx
Regards
Poonam
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.