The Basics of Templates in Server Controls
page 3 of 6
by Justin Lovell
Feedback
Average Rating: 
Views (Total / Last 10 Days): 33765/ 54

Guidelines and Trends… and Tips

Now that we have looked at a simple example, I can declare that the same guidelines and trends apply when creating composite controls… in addition:

 

The containing control should inherit from a basic control or a template server control

 

This is a fast and hard guideline. If your control inherits from the Panel control or similar controls, then expect the functionality to break. The reason is: with the attributes that are applied to the Panel class, makes the text inside the control to be treated as child controls… not as part of the containing control (as what the previous sample demonstrated and discussed briefly on).

 

Create a property and with a value type of ITemplate interface.

 

This is to allow the other control developers to extend your template based control. Your template should be a “pluggable” property – either can be set programmatically or my mark-up: the later was demonstrated on the first page of this article and the other (the first mention) will be demonstrated later in the article. There is one exception for this trend:

 

If you need to provide a type of default template for the template property, follow the same rules as if the ITemplate was a server control.

 

There will be some times that you will want a “default” template. This situation will only occur if one is trying to programmatically set the template property. Instead of having an ITemplate interface as the property’s data type, the template should be a class.

 

This template class, that implements the ITemplate interface, should follow the same general guidelines and trends as if it is a server control. The reason for this is that you do not want to limit the extension of your server control at all (and hiding templates should not hinder the consistency at all). For example, let’s say that you had a template class:

 

public class MyTemplate : ITemplate {
   public virtual void InstantiateIn(Control container) {
      Label label = new Label();
 
      // ...
 
      container.Controls.Add(label);
   }
}

 

The “template class” should have the ability to be extended such as (just in the same fashion as with actual server controls):

 

public class MyExtendedTemplate : MyTemplate {
   public overrides void InstantiateIn(Control container) {
      Button button = new Button();
 
      // ...
 
      base.InstantiateIn(container);
      container.Controls.Add(button);
   }
}

 

The properties that expose the templates should have the getter and setter methods.

 

This is required by ASP.NET. Do not fall into this trap because this is a very common mistake made by control developers and normally find themselves dumb-founded when ASP.NET throws an error.

 

All controls created by the template, should not have an ID set

 

There may be problems if you set the ID’s of the controls inside the template, to something. Rather let ASP.NET control the ID for the controls inside the template.

 

The container control should implement the INamingContainer interface

 

Brutally said, this is to avoid complications with the previous guideline. Things can get hairy without this interface and most of the time; this will bail you out of the mess. Obviously, this “hair” will only grow if the controls inside the template actually do post backs to the server.

 

Suffix your property name with “Template” if it exposes a template

 

This is a naming convention for your properties. For example, if you had a control that you wanted to expose a template property by the name of “Foo”, then the name of the property should be “FooTemplate”. This describes what the property is and also keeps consistency with the framework (you have the ItemTemplate template in the Repeater control and so forth).

 

Always create new instances of controls in the InstantiateIn method

 

This is a trap that most developers fall into when creating “repeater” controls. This is just a guideline and the reason for people running into this trap is for the simple reason that .NET handles all variables as pointers/references instead of objects.


View Entire Article

User Comments

Title: Mr   
Name: Graham J
Date: 2007-02-10 2:10:59 PM
Comment:
Thanks Justin, the MSDN class details regarding FormView and Item Template's didn't really delve into creating the control within C# alone. Plus I couldn't establish how to bind a textbox to my datasource for an insert. I decided to scour the net to see what scraps of information I could find. But when I found this page, I saw I wouldn't have to look any futher - you've answered all my questions in one place. Thank you much-o!
Title: question on adding templates   
Name: Peter Kellner
Date: 2006-08-12 12:22:37 PM
Comment:
I'm a little confused. I added your downloaded controls to my project, dragged in the SimpleTemplateControl to my design surface, and I expected to see a template automatically created. It was not. What am I not understanding?

Thanks, for a great article.

-Peter
Title: love this   
Name: cyberguest
Date: 2006-02-21 10:28:30 AM
Comment:
thanks, great article with simple, easy to understand examples
Title: Missing VB code   
Name: JP
Date: 2005-08-03 4:27:50 AM
Comment:
What happened to showing VB code as was done in previous articles ?
Title: Excellent   
Name: Muhammed YAseen
Date: 2005-07-05 9:24:59 AM
Comment:
I forgot to go the next page there i found how to add childcontrols at runtime
Title: Excellent   
Name: Muhammed YAseen
Date: 2005-07-05 9:23:44 AM
Comment:
This is an excellent way of adding controls in a container control. Can we do this at runtime? I mean adding child controls at runtime.
Title: Thanks   
Name: DC
Date: 2005-03-20 5:59:19 PM
Comment:
Thanks Justin, as usual your article are very helpfull.
Can't wait for the next article ;-)
Title: Excellent   
Name: Reng van Oord
Date: 2004-08-27 4:14:09 AM
Comment:
I'm learning a lot reading these articles. Thanks a lot!

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-08 7:21:02 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search