Com(p)fy
Skip Navigation Links >> Community >> Technology Forums >> Compfy >> Web Development >> ASP.NET >> Server-Side >> Managing dynamically loaded ASP.NET User Controls
Contents
Skip Navigation Links.
CollapseFolder
Web Development ( Posts: 5 Last: 5/22/2010 )
CollapseFolder
ASP.NET ( Posts: 3 Last: 5/22/2010 )
CollapseFolder
Server-Side ( Posts: 2 Last: 5/22/2010 )
Text Post
Text Post
ExpandFolder
Client-Side ( Posts: 1 Last: 4/21/2010 )
ExpandFolder
ADO.NET ( Posts: 1 Last: 4/21/2010 )
ExpandFolder
Components and Libraries ( Posts: 1 Last: 5/21/2010 )
Folder
ExpandFolder
Compilers ( Posts: 1 Last: 6/20/2010 )
ExpandFolder
 
Managing dynamically loaded ASP.NET User Controls
4/21/2010 11:28:24 AM Last modified: 5/22/2010 11:14:49 PM
Greg
Joined: 4/17/2010
Posts: 6

When loading ASP.NET User Controls dynamically, we need to reconstruct them for processing postback events. Microsoft recommends recreating dynamic controls in the PreInit page event. Here is a convenient way of doing it. We create a class that will load dynamic controls, save information about them into the ViewState of the parent control, and restore the dynamic controls on request.

First we need properties for saving and exposing the parent control, the StateBag of the parent control, and the list of the loaded controls.

Code
            private const string _vsString = "CompfyDynamicControls";

            public Control Parent { get; private set; }
            public StateBag ViewState { get; private set; }

            public List<Pair> LoadedControls
            {
                get
                {
                    if (ViewState[_vsString] == null)
                        ViewState[_vsString] = new List<Pair>();
                    return (List<Pair>)ViewState[_vsString];
                }
            }
 

In the constructor (see "Controls" below), we initialize the Parent control and the ViewState properties. The code snippet below also shows some other functions that can be added for convenience.

Code
            public Controls(Control parentControl, StateBag viewState)
            {
                Parent = parentControl;
                ViewState = viewState;
            }

            public bool IsEmpty
            {
                get
                {
                    if (ViewState[_vsString] == null)
                        return true;
                    if (((List<Pair>)ViewState[_vsString]).Count == 0)
                        return true;
                    return false;
                }
            }

            public void Clear()
            {
                ViewState.Remove(_vsString);
            }
 

After the class is initialized, we can call the LoadControl method, which loads the dynamic controls, adds them to the container control such as a PlaceHolder control, and saves the ID of the container control and the physical path to the dynamic user control in the list of controls maintained in the StateBag of the parent control. Please notice how one can generate the ID for the newly loaded dynamic control.

Code
            public Control LoadControl(Control container, string controlPath)
            {
                Control newcontrol = Parent.Page.LoadControl(controlPath);
                if (newcontrol != null)
                {
                    newcontrol.ID = controlPath.Replace(".", "").Replace("/", "").Replace("~", "") +
                            container.Controls.Count.ToString();
                    container.Controls.Add(newcontrol);
                    LoadedControls.Add(new Pair(container.ID, controlPath));
                }
                return newcontrol;
            }
 

During the Postback event, the parent control calls the RestoreControls method in its Page_Init event handler, for example. The ResoreControls method accesses the list of the dynamic controls stored in the ViewState, re-loads them, and adds them to the container controls identified by the stored control ID's.

Code
            public void RestoreControls()
            {
                if (ViewState[_vsString] == null) return;

                foreach (Pair pair in LoadedControls)
                {
                    Control container = Parent.FindControl((string)(pair.First));
                    if (container != null)
                    {
                        string controlPath = (string)(pair.Second);
                        Control nc = Parent.Page.LoadControl(controlPath);
                        if (nc != null)
                        {
                            nc.ID = controlPath.Replace(".","").Replace("/","").Replace("~","") +
                                    container.Controls.Count.ToString();
                            container.Controls.Add(nc);
                        }
                    }
                }
            }
 

Here are examples of using this class. In the first example, we load a dynamic control. In the second, we restore dynamic controls. In the third, we remove all dynamic controls in the parent control.

Code
DC.Controls sc = new DC.Controls(this, ViewState);
sc.LoadControl(DataViewPlaceHolder, "ForumView.ascx");
 

 

Code
            DC.Controls sc = new DC.Controls(this, ViewState);
            if (IsPostBack)
            {
                sc.RestoreControls();
                return;
            }
 

 

Code
            DC.Controls sc = new DC.Controls(this, ViewState);
            sc.LoadedControls.Clear();
 

 

Replies: Page: First Last  Posts/Page:
  Page: First Last  Posts/Page:


Registered users:
8
Users online:
1
Folders total:
18
Posts total:
6
Server timezone (for anonymous users):
(UTC-08:00) Pacific Time (US & Canada)
Powered by Com(p)fy