Events and postback with dynamic ASP.Net user controls

I’ve been trying to build an editable organisation chart for an intranet project and have built up a structure of nested dynamic user controls. This was all working well for displaying the information but wasn’t working so well for posting back and registering events.

A bit of Googling later and I found this article on the 4 Guys From Rolla site called Dynamic Web Controls, Postbacks, and View State which explained the general gist of it.

The cause of the problem was that if you bind the data that loads the controls in page load then the events don’t fire, but if you don’t bind it there’s no data to register the event on. The solution? Do the binding in the init function instead. It worked a treat.

Dynamic Web Controls, Postbacks, and View State