DynamicControlsPlaceholder - A placeholder that saves its child controls


ASP.NET gives a developer the opportunity to programmatically add controls to a web form using ParentControl.Controls.Add(new Control());
However, these controls are not persisted in any way thus having to be recreated for each subsequent request.


To create a control that behaves like a placeholder but additionally handles recreating dynamic controls on subsequent requests.


I have created a custom control called DynamicControlsPlaceholder that derives from Placeholder and overrides Load- and SaveViewState.
In SaveViewState, the control hierarchy is recursively traversed and the control type and ID persisted to a string
In LoadViewState the persisted information is used to recreate the control tree to the state before.

Download (V2.2):


V2.2.0.0 (published 2006-05-12)

Bugfix: Under some conditions, UserControls still could not be reloaded when the application is configured as a website

V2.1.0.0 (published 2006-01-11)

Bugfix: UserControls could not be reloaded when the application is configured as a website (as opposed to virt.dir.)

V2.0.0.0 (published 2005-12-18)

Bugfix: resolved compatibility issue with ASP.NET 2.0

Feature: enabled support for UserControls with special characters in the filename

V1.1.0.0 (published 2003-09-24)

Bugfix: excluded child control persistance for CheckBoxList (ListItems are automatically persisted in statebag)

V1.0.2.0 (published 2003-05-29)

Bugfix: problem with UserControls whose filename includes "_"

V1.0.1.0 (published 2003-02-02)

added events: ControlRestored, PreRestore, PostRestore

added property: ControlsWithoutIDs (specifies how to handle controls without ID)

V1.0.0.0 (published 2003-01-11)

Initial version

Known Problems:

1. Property values are only saved in Viewstate if control was added to parent

Please note that properties, that are set before TrackViewState is called, are lost. As an example, imagine you create a Listbox and add Listitems before actually adding it to the control tree. In the following roundtrip, the Listitems are not recreated from ViewState as they are not stored in it.


		DropDownList ddl = new DropDownList();
		ddl.ID = "DDL1";
will result in an empty Listbox after a roundtrip, whereas
		DropDownList ddl = new DropDownList();
		ddl.ID = "DDL1";
will work, as TrackViewState is called in Controls.Add and changes to properties are persisted from then on.

2. Delegate restoration

Please note that delegates are currently not persisted. This means that when you are connecting an event handler with a control's event, the event handler may not be called after a postback. Example:
		TextBox tb = new TextBox();
		ddl.ID = "TB1";
		//this is not automatically restored
		tb.TextChanged += new EventHandler(tb_OnTextChanged);

DynamicControlsPlaceholder exposes two events that can be used to manually reattach delegates.
		private void DCP_ControlRestored(object sender, 
			DBauer.Web.UI.WebControls.DynamicControlEventArgs e)
			if(e.DynamicControl.ID == "TextBox1")
				((TextBox) e.DynamicControl).TextChanged 
					+= new EventHandler(tb_OnTextChanged);
The above example shows how to use the ControlRestored event that is raised for each control on the Placeholder. Another option would be to use the PostRestore event that is raised after all controls on the Placeholder have been restored
		private void DCP_PostRestore(object sender, System.EventArgs e)
			TextBox tb = (TextBox) DCP.FindControl("TextBox1");
			tb.TextChanged += new EventHandler(tb_OnTextChanged);


If you have any comments, suggestions or questions, feel free to drop me a line.