Click here to Skip to main content
11,631,369 members (69,919 online)
Click here to Skip to main content

ViewState management for dynamically loaded UserControls

, 30 Apr 2007 CPOL 42.1K 738 16
Rate this:
Please Sign up or sign in to vote.
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 Smile | :)

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 Smile | :) 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)

Share

About the Author

Wesam Elsawirki
Software Developer (Senior)
Hungary Hungary
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

You may also be interested in...

Comments and Discussions

 
GeneralMessage Automatically Removed Pin
24-Nov-11 1:10
memberAytha24-Nov-11 1:10 
GeneralMore about Dinamically added controls Pin
rafael.aca14-Apr-09 12:06
memberrafael.aca14-Apr-09 12:06 
GeneralDynamice Usercontrol .ascx Pin
Kurmala13-Jul-08 19:50
memberKurmala13-Jul-08 19:50 
GeneralUser Controls Piling UP on Page [modified] Pin
stanz927-Feb-08 7:04
memberstanz927-Feb-08 7:04 
GeneralIs this a sure tested method of loading usercontrols dynamically Pin
xsoftdev19-Feb-08 13:12
memberxsoftdev19-Feb-08 13:12 
GeneralPass values to other user controls Pin
xsoftdev18-Oct-07 15:55
memberxsoftdev18-Oct-07 15:55 
Hello,

I was wondering how you can pass values between the dynamically loaded user controls.

Say I have a static user control (UC1) on my page which has a Dropdownlist.

I add another control dynamically (UC2) into a placeholder on the page.

On the selected index changed for the Dropdownlist in UC1 I want to fire an event which will make a panel visible in UC2.

An ideas on how I can do this?

Thanks
GeneralRe: Pass values to other user controls Pin
Wesam E.20-Oct-07 3:56
memberWesam E.20-Oct-07 3:56 
GeneralRe: Pass values to other user controls Pin
xsoftdev26-Nov-07 6:52
memberxsoftdev26-Nov-07 6:52 
GeneralLoad one and Unload the others Pin
xsoftdev4-Oct-07 9:00
memberxsoftdev4-Oct-07 9:00 
GeneralRe: Load one and Unload the others Pin
Wesam E.8-Oct-07 20:04
memberWesam E.8-Oct-07 20:04 
GeneralUserControl Pin
Brother Louis20-Aug-07 0:54
memberBrother Louis20-Aug-07 0:54 
GeneralRe: UserControl Pin
Wesam E.8-Oct-07 19:59
memberWesam E.8-Oct-07 19:59 
Questionquestion from manish.2004 Pin
Sean Ewington1-Jun-07 9:04
adminSean Ewington1-Jun-07 9:04 
AnswerRe: question from manish.2004 Pin
Wesam E.4-Oct-07 20:23
memberWesam E.4-Oct-07 20:23 
GeneralRe: question from manish.2004 Pin
xsoftdev22-Nov-07 0:50
memberxsoftdev22-Nov-07 0:50 
QuestionIssues at Page PostBack Pin
mkhiami27-Apr-07 10:40
membermkhiami27-Apr-07 10:40 
QuestionThread-Safe ??? Pin
Marco M.18-Apr-07 9:06
memberMarco M.18-Apr-07 9:06 
AnswerRe: Thread-Safe ??? Pin
Johnno7423-Apr-07 16:57
memberJohnno7423-Apr-07 16:57 
GeneralRe: Thread-Safe ??? Pin
Wesam E.30-Apr-07 0:08
memberWesam E.30-Apr-07 0:08 
GeneralRe: Thread-Safe ??? Pin
Johnno7430-Apr-07 12:50
memberJohnno7430-Apr-07 12:50 
GeneralControl issue Pin
Volleyknaller17-Apr-07 22:39
memberVolleyknaller17-Apr-07 22:39 
GeneralRe: Control issue Pin
Wesam E.18-Apr-07 0:35
memberWesam E.18-Apr-07 0:35 
GeneralRe: Control issue Pin
Volleyknaller18-Apr-07 1:18
memberVolleyknaller18-Apr-07 1:18 
QuestionPostback issue Pin
inetfly12317-Apr-07 11:01
memberinetfly12317-Apr-07 11:01 
AnswerRe: Postback issue Pin
Wesam E.17-Apr-07 19:59
memberWesam E.17-Apr-07 19:59 

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 | Terms of Use | Mobile
Web03 | 2.8.150723.1 | Last Updated 30 Apr 2007
Article Copyright 2007 by Wesam Elsawirki
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid