Skip navigation.

New Jersey Code Camp II RecapAll recent postsAwesome Firefox extension: FireBug

How to Preserve Control State

At times, you need to extend a server control to add functionality. For example, I found the Wizard control lacking a property which indicated what the previous step was. In other words, when the ActiveStepChanged event is raised, can you tell which way the user was going through the wizard (forward, by clicking Next; or backward, by clicking Previous)?

Granted, the PreviousButtonClick and NextButtonClick events allow you to inspect the current and next steps, but there’s a bigger issue here.

Don’t Make Me Copy & Paste

Many control developers and vendors would write documentation and post samples of how you set up a host of properties on a control. That’s great, except when it comes to using a control on a number of pages, the cut-and-past routine becomes counterproductive. Think about maintaining all this markup goo.

The wizard control gave me headaches because I could not customize its button text with localized expressions in skins, so I was forced to derive from Wizard a control of my own.

Maintaining State

In the provided source code you will see a property, PreviousStepIndex, which indicates where the user was before he/she came to the current “step”. Now the question is where to store it.

Initially, I wanted to use control state. Control state is an awesome 2.0 addition, and it’s different from view state.

MSDN has a sample of how to save and restore control state, but it’s incorrect. If you derive from any control, other that Control itself, there’s a good chance you will nuke its state if don’t bother saving and restoring it for the parent control.

For example, if I were to store PreviousStepIndex in control state and simply follow the said sample, I’d lose whatever the parent control, Wizard, needs to maintain (which, in fact, is exactly what happened). In my code, I create a new Pair and stuff the Wizard’s control state (if any) and mine in it.

I don’t know if I’m doing it the hard way, but the takeaway is this: if you derive from a control, be mindful of its state. Don’t just overwrite it.

Comments

Comment permalink 1 Web Developer India |
First of all thanks for sharing Preserving Control State. Few days before when I was trying Free Text Editor as server control I was facing problem with preserving control state with the typed text in the WYSIWYG editor. But after your article and reference to Microsoft Tech Notes I presume that I will solve my problem.
Thanks
Andy
Comment permalink 2 VB Guest |
I am using viewstate for the custom controls with their id as indexer for viewstate. I should stop now and start using the ControlState. thank you for sharing
Comment permalink 3 VB Calculator |
we now a days minimizing the viewstate usage because of the loading time and page size. something to be done for alternatives.
Comment permalink 4 VB Calculator |
i forgot, we donot have option for session state also. but currently preserving page data in a db.
Comment permalink 5 ahmed |
thank you

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):