Again asked on forums:
http://forums.asp.net/1369575/ShowThread.aspx
Question:
I'm trying to make a template appear at design time when I drag the control onto the surface. I'm using an article from asp alliance sample whose simple template control I've pasted below. I'm expecting the template to show up when I drag it onto the design surface but it does not. Any ideas?
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
namespace TemplateControls {
[ToolboxData("<{0}:SimpleTemplateControl runat=server></{0}:SimpleTemplateControl>")]
[ParseChildren(true)]
[PersistChildren(false)]
public class SimpleTemplateControl : Control, INamingContainer {
private ITemplate pSimpleTemplate;
#region Properties
[Browsable(false)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate SimpleTemplate {
get { return pSimpleTemplate; }
set { pSimpleTemplate = value; }
}
#endregion
#region Methods
protected override void CreateChildControls() {
Controls.Clear();
if (SimpleTemplate != null)
SimpleTemplate.InstantiateIn(this);
}
#endregion
}
}
My reply:
you need to implement a designer for your control.
using System.Web.UI.Design;
using System.ComponentModel;
/// <summary>
/// Summary description for SimpleTemplateControlDesigner
/// </summary>
namespace TemplateControls
{
public class SimpleTemplateControlDesigner : ControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
SetViewFlags(ViewFlags.TemplateEditing, true);
}
public override TemplateGroupCollection TemplateGroups
{
get
{
TemplateGroupCollection templateGroups = new TemplateGroupCollection();
SimpleTemplateControl control = (SimpleTemplateControl)this.Component;
TemplateGroup group = new TemplateGroup("Item");
TemplateDefinition template = new TemplateDefinition(this, "Template", control, "SimpleTemplate", false);
group.AddTemplateDefinition(template);
templateGroups.Add(group);
return templateGroups;
}
}
}
}
Note that you need to reference System.Design.dll assembly in order to get access to ControlDesigner. You attach the designer to your control with Designer attribute
[ToolboxData("<{0}:SimpleTemplateControl runat=server></{0}:SimpleTemplateControl>")]
[ParseChildren(true)]
[PersistChildren(false),
Designer(typeof(SimpleTemplateControlDesigner))
]
public class SimpleTemplateControl : Control, INamingContainer
...
**
Note: Creating templated control designers in ASP.NET 2.0 is one example where there has been significant improvement since version 1.x. Creating designers is now super-easy!
Let's discuss a bit what the few lines of code mean.
1. public class SimpleTemplateControlDesigner : ControlDesigner
You inherit your designer from base class of all asp.net web control designers, that is, System.Web.UI.Design.ControlDesigner.
2. public override void Initialize(IComponent component)
{
base.Initialize(component);
SetViewFlags(ViewFlags.TemplateEditing, true);
}
Enables template editing at design-time
3. public override TemplateGroupCollection TemplateGroups
{
get
{
TemplateGroupCollection templateGroups = new TemplateGroupCollection();
SimpleTemplateControl control = (SimpleTemplateControl)this.Component;
TemplateGroup group = new TemplateGroup("Item");
TemplateDefinition template = new TemplateDefinition(this, "Template", control, "SimpleTemplate", false);
group.AddTemplateDefinition(template);
templateGroups.Add(group);
return templateGroups;
}
}
}
}
First, we create a new TemplateGroup with name "Item". I left the name that way so that you can see where in the design-time view it has impact on. It is shown in the list of editable templates in a drop-down list.
Next. I created TemplateDefinition which takes the designer instance, name of the template "Template" (also left that way so that you see where it impacts) to show in the editing region, controls instance, name of the template property on the control plus a boolean flag to indicate if only server controls (true) or any content like literal (false) is allowed into the template.
The thing is then returned within TemplateGroupCollection instance.