Indeed, you are right. We should simplify the implementation of Control Designers and we will try to make it simpler for the next version.
The base ControlDesigner class is intended to be used in class libraries and therefore it does not inherit form UserControl but from CompositeControl. Maybe we should provide another implementation that inherits from UserControl. However, there are some helper classes that you may find useful. I will explain them later.
Let’s start with defining a designer for your control. A better way to specify designer for your control is by attribute at the class level:
The web.config is an alternative method and it is provided to allow specifying designers for third party controls.
Retrieving and setting the underlying control properties shouldn’t be so terribly complicated if you can reference the type of the control at compile time. In fact, designers are usually made for a particular control only and shouldn’t be a problem to cast DesignedControl
property to the type of the control you are designing and retrieve and set values directly from its properties. Unfortunately, this is not as simple for User Controls as their assemblies are compiled dynamically and you can’t reference them until run time. So the workaround is to create a base class in App_Code inheriting from UserControl, expose the properties you need and then make your actual .ascx to inherit from your base class.
Also I’d like to make a note about creating template controls. You should not instantiate the template directly in the current control. Instead, you should create a container for the template and instantiate in the container. Then you should set control properties and subscribe for the events you need and at the end you should add the container to the controls collection of the current control. The reason for that is because the current control is already added to the controls collection of the page and it is at certain stage of the page lifecycle. As you add controls to its collection, the controls will immediately try to catch-up and depending on the current stage, their events might be fired before you subscribe for them. In this particular case, this is not a problem because CreateChildControls method is called at the right moment but this is not necessarily always the case.
We have provided a base class for container to make it easier to find controls within it. I have modified and attached the example so you can examine the changes.
I hope this will be helpful for you.
the Telerik team