[karsten samaschke]

ASP.NET daily. Or weekly.

Recreate the controls!

Two very important rules if you work with dynamically added controls:

Recreate the controls!
Recreate every single control on PostBack if you want to react on any events. If you "forget" just one control, your ViewState may become invalid and no event will be fired or the page might behave as if there was no PostBack.

Recreate controls as early as possible!
Additionally, if you want to add controls to a page, usually you would add them in Page_Load-method. This is early enough if you don't need to capture events of the dynamically added controls. But if you want to capture events, you'll have to recreate them as early as possible. The most promising method is Page_Init, which is called before Page_Load and before controls fire their events. So: Use Page_Init instead of Page_Load when adding controls with events to your page.

Comments

Scott Galloway said:

I always recommend the use of Denis Bauer's DynamicControlsPlaceholder for this, it basically lets you forget about recreating the controls - you have to re-hookup the events but it makes things a less painful see: http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
# December 16, 2003 6:07 PM

Jesse Ezell said:

Why not create them in CreateChildControls like everything else? Page_Init is definately not good, because you are adding an additional event to your page. Always prefer overrides to events when subclassing (if you must use init, use OnInit).

You don't really have to create the controls every time either, but you should always assign IDs to them so that your events will work properly. Otherwise, you have random IDs which will change if the number of controls on the page changes for some reason (which may not be because of anything your code is doing).

However, regardless of all this, you shouldn't add controls dynamically to a page, because it leads to nasty error messages. Not only can you get the invalid viewstate one if you are not careful, but you can't insert <%= "value" %> blocks and other nice things in your pages. Use user controls or composite controls, that is what they are there for.
# December 16, 2003 6:21 PM

Darren Neimke said:

Ditto Scott's recommendation for DynamicControlsPlaceholder. I've used it in a pretty large application that I developed recently and found it to be excellent!
# December 16, 2003 6:29 PM

Wasim Kazi said:

Ive seen may examples of Dynamic Loading of webcontorls and handling events etc.. But I have a little different problem to deal with. In most provided solutions on websites the classname of the control that needs to be loaded is know at design time. Lets say I have a n number of usercontrols whose classnames and paths are stored in the database. On a page home.aspx, I want these controls to appear and behave normally as a control would.
Key point to note is that I do not have any infomation about the control at design time. How can i do this?

# January 1, 2004 2:28 AM

ffff said:

ff
# April 30, 2004 9:58 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)