Click here to Skip to main content
Licence CPOL
First Posted 17 Apr 2007
Views 30,950
Downloads 358
Bookmarked 16 times

ViewState management for dynamically loaded UserControls

By | 30 Apr 2007 | Article
ViewState management for dynamically loaded UserControls.

Introduction

In many of our business ASPX applications, there is a need for special UserControls that perform specific functionalities so that they can be easily re-used in the entire application. There are two ways we can use a UserControl: either we drag it onto the form, or we load it dynamically through the use of LoadControl("MyUserControl.ascx"). When we use the second method, we (all?) encounter one major problem: ViewState persistence! Upon postback, when we re-load the control in our CS code, .NET Framework forgets everything about its previous state. This tiny essay addresses this issue.

Solution!

Straightforward: assign the dynamically loaded control the same ID it had just before the data postback! In fact, .NET Framework retains the state of every user control (even of those dynamically loaded) and associate them with their programmatic IDs; after a postback, it tries to find the same controls with the same IDs in order to load their previous state. The whole thing can better be explained through an example :-)

Example

We'll create a simple UserControl which has a TexBox, we'll also have an ASPX form on which we use this control but in three ways. For this purpose, we'll create three instances:

  • An instance which is already dragged onto the form
  • An instance dynamically created through LoadControl()
  • An instance dynamically created through LoadControl() with its ID preserved

I have created a simple UserControlViewStateManager that will help us automatically save/load the dynamically loaded controls and their IDs. The main idea behind this gadget is to save the ID of the UserControls right after loading them into their parent container and re-creating them after the postback: the best place for re-creating the controls is the Page_Init event.

When creating a new user control: just give a unique ID and at the same time, add it to an instance of the UserControlViewStateManager. Shown below is a snippet:

Control userControl = LoadControl("MyUserControl.ascx");
userControl.ID = (placeHolder2.Controls.Count - 1).ToString();
placeHolder2.Controls.Add(userControl);
ucViewStateManager.AddControl(userControl);

In the Page_Init event, call ReCreateControlsID() of the helper class, passing it the container object as a parameter:

ucViewStateManager = new UserControlViewStateManager("SomeUniqueSessionID");
ucViewStateManager.ReCreateControlsID(placeHolder2);

Hope this helps :-) In the demo application, you can find the entire source code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Wesam Elsawirki

Software Developer (Senior)

Hungary Hungary

Member

Professional Software Developer, currently working for HVB bank.
Programming experience includes: C#, ASP.NET, Delphi, PHP, VB.
Technologies: MS SQL Server 2005, Apache, IIS.
 
Certifications: MCAD, MCSD, MCTS (MS SQL Server 2005), MCITP

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberAytha1:10 24 Nov '11  
GeneralMore about Dinamically added controls Pinmemberrafael.aca12:06 14 Apr '09  
GeneralDynamice Usercontrol .ascx PinmemberKurmala19:50 13 Jul '08  
GeneralUser Controls Piling UP on Page [modified] Pinmemberstanz97:04 27 Feb '08  
GeneralIs this a sure tested method of loading usercontrols dynamically Pinmemberxsoftdev13:12 19 Feb '08  
GeneralPass values to other user controls Pinmemberxsoftdev15:55 18 Oct '07  
GeneralRe: Pass values to other user controls PinmemberWesam E.3:56 20 Oct '07  
GeneralRe: Pass values to other user controls Pinmemberxsoftdev6:52 26 Nov '07  
GeneralLoad one and Unload the others Pinmemberxsoftdev9:00 4 Oct '07  
GeneralRe: Load one and Unload the others PinmemberWesam E.20:04 8 Oct '07  
GeneralUserControl PinmemberBrother Louis0:54 20 Aug '07  
GeneralRe: UserControl PinmemberWesam E.19:59 8 Oct '07  
Questionquestion from manish.2004 PinadminSean Ewington9:04 1 Jun '07  
AnswerRe: question from manish.2004 PinmemberWesam E.20:23 4 Oct '07  
GeneralRe: question from manish.2004 Pinmemberxsoftdev0:50 22 Nov '07  
QuestionIssues at Page PostBack Pinmembermkhiami10:40 27 Apr '07  
QuestionThread-Safe ??? PinmemberMarco M.9:06 18 Apr '07  
AnswerRe: Thread-Safe ??? PinmemberJohnno7416:57 23 Apr '07  
GeneralRe: Thread-Safe ??? PinmemberWesam E.0:08 30 Apr '07  
GeneralRe: Thread-Safe ??? PinmemberJohnno7412:50 30 Apr '07  
GeneralControl issue PinmemberVolleyknaller22:39 17 Apr '07  
GeneralRe: Control issue PinmemberWesam E.0:35 18 Apr '07  
GeneralRe: Control issue PinmemberVolleyknaller1:18 18 Apr '07  
QuestionPostback issue Pinmemberinetfly12311:01 17 Apr '07  
AnswerRe: Postback issue PinmemberWesam E.19:59 17 Apr '07  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 30 Apr 2007
Article Copyright 2007 by Wesam Elsawirki
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid