Enabling Design Time Template Editing

by Amr 3/3/2008 9:28:00 PM

Few days ago, I blogged about how to enable auto formats in the design time smart tag by overriding simple property, this post will describe how to enable design time template editing in a similar simple way.

Template Editing 

The following steps will show how to do this in your template controls, assuming you already have a template control ( template controls are web server controls that have one or many properties of type ITemplate ).

So, this is how it works :

  1. Implement a custom designer that inherits from System.Web.UI.Design.ControlDesigner
  2. Override the TemplateGroups Collection.
  3. In the get define your template group collection and return it.
  4. Don't forget to enable TemplateEditing flag.

The Sample I introduce here contains a template control having 4 templates which I divide them into 2 template groups.

The SampleControl

[ToolboxData("<{0}:SampleControl runat=server></{0}:SampleControl>")]
[Designer(typeof(SampleControlDesigner))]
public class SampleControl : TemplateControl
{
    private ITemplate _firstTemplateX;
    private ITemplate _firstTemplateY;
    private ITemplate _secondTemplateX;
    private ITemplate _secondTemplateY;


    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate FirstTemplateX
    {
        get { return _firstTemplateX; }
        set { _firstTemplateX = value; }
    }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate FirstTemplateY
    {
        get { return _firstTemplateY; }
        set { _firstTemplateY = value; }
    }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate SecondTemplateX
    {
        get { return _secondTemplateX; }
        set { _secondTemplateX = value; }
    }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate SecondTemplateY
    {
        get { return _secondTemplateY; }
        set { _secondTemplateY = value; }
    }

}

The Control Desginer

class SampleControlDesigner : ControlDesigner
{
    public override void Initialize(IComponent component)
    {
        base.Initialize(component);
        SetViewFlags(ViewFlags.TemplateEditing, true);
    }

    private TemplateGroupCollection _templateGroups;

    public override TemplateGroupCollection TemplateGroups
    {
        get
        {
            if (_templateGroups == null)
            {
                _templateGroups = new TemplateGroupCollection();

                TemplateGroup tempGroup1, tempGroup2;
                TemplateDefinition tempDef1, tempDef2, tempDef3, tempDef4;
                SampleControl ctl;

                ctl = (SampleControl)this.Component;

                tempGroup1 = new TemplateGroup("FirstTemplateGroup");
                tempGroup2 = new TemplateGroup("FirstTemplateGroup");

                tempDef1 = new TemplateDefinition(this, "TemplateX", ctl, "FirstTemplateX", true);
                tempDef2 = new TemplateDefinition(this, "TemplateY", ctl, "FirstTemplateY", true);
                tempDef3 = new TemplateDefinition(this, "TemplateX", ctl, "SecondTemplateX", true);
                tempDef4 = new TemplateDefinition(this, "TemplateY", ctl, "SecondTemplateY", true);

                tempGroup1.AddTemplateDefinition(tempDef1);
                tempGroup1.AddTemplateDefinition(tempDef2);
                tempGroup2.AddTemplateDefinition(tempDef3);
                tempGroup2.AddTemplateDefinition(tempDef4);

                _templateGroups.Add(tempGroup1);
                _templateGroups.Add(tempGroup2);
            }

            return _templateGroups;
        }
    }
}

The final output of the sample looks like this

Final output of sample

Here's the code so have fun.

SampleControl.cs (3.32 kb)

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Design Time Support

Related posts

Comments

3/4/2008 7:02:34 PM

pingback

Pingback from blog.cwa.me.uk

Reflective Perspective - Chris Alcock » The Morning Brew #45

blog.cwa.me.uk

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

5/16/2008 2:06:49 PM

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About the author

Amr Elsehemy Amr Elsehemy
MCSD, MCTS Sql 2005 E-mail me Send mail

Calendar

<<  May 2008  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Creative Commons
Sign in

Sitemeter