ASP.NET: Implementing designer for a templated server control

Posted by: Jotekes Blog, on 14 Aug 2006 | View original | Bookmarked: 0 time(s)

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.

 

 

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 4656 | Hits: 243

Similar Posts

  • Adding users to a TFS project when youre not on the domain more
  • Going Controller-less in MVC: The Way Fowler Meant It To Be more
  • XAML By FARR: Resource Dictionaries Vs User Control Resources more
  • CodeDigest.Com Article,Codes,FAQs - April,2009 more
  • Stimulsoft Reports Designer.Web. Easy work. more
  • Easy way to create a web-based AJAX SFTP Client application more
  • Put Your Pages and Views on Lockdown more
  • Web Application Project generated CodeBehind .designer file not being updated more
  • March's Toolbox Column Now Online more
  • ASP.NET Data Control Events more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD