Saturday, July 5, 2008

DynamicControlsPlaceholder - A placeholder that saves its child controls

Problem:

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.

Goal:

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

Procedure:

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

From: http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

1 comment:

azam said...

I tried that control in datagrid, but I am facing a problem.

I created a custom validator control, a text box control. then I set "controltovalidate" property of customer validator control.
I added both controls to same DynamicControlsPlaceholder control instance.

On Postback, it is throwing a exception.
Controltovalidate property of cust_val_219 cannot be blank.

whereas cust_val_219 is the ID of my custom validator control.

I think it is not persisting controltovalidate property? any idea?