Programmatically Loading TemplatedControl Templates

One of the tricks I learned a while back was that, when using any kind of templated server control, you can put the template definition in a user control, and load it programmatically using Page.LoadControl(controlLocation). This is particularly useful in large web applications, where you may use the same template over and over again. This way, if you make a change to one, it gets propagated to all instances.

It's also useful to note that Page.LoadControl instantiates the user control's class, so any events, like the Load event, get fired. Initially, I thought that maybe it just parsed the text in the control and instantiated what's in the text, but thankfully, that isn't the case. It's extremely important in my situation, because the template I'm loading contains it's own templated control that must programmatically load a template.

At any rate, hope this information is useful to someone.

1 Comment

Comments have been disabled for this content.