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

A Simple Example

Let’s introduce the simple code for adding controls that are in templates to the control collection:

 

[ParseChildren(true)]
[PersistChildren(false)]
public class SimpleTemplateControl : Control, INamingContainer {
   private ITemplate pSimpleTemplate;
 
   [Browsable(false)]
   [PersistenceMode(PersistenceMode.InnerProperty)]
   public ITemplate SimpleTemplate {
      get { return pSimpleTemplate; }
      set { pSimpleTemplate = value; }
   }
 
   protected override void CreateChildControls() {
      Controls.Clear();
 
      if (SimpleTemplate != null)
         SimpleTemplate.InstantiateIn(this);
   }
}

 

First, we will look at the attributes applied to the control. These attributes are required to be applied to the control class so the ASP.NET parser treats the text inside the control as part of the control; therefore, the page parser can designate the template to be “built”. These attributes are already implemented on the WebControl class so it may not be necessary for you to apply these attributes.

 

The template of the above example is the SimpleTemplate property. There is a Browsable attribute attached to the SimpleTemplate property to tell the IDE to that it may not be visible to the property explorer. The PersistanceMode attribute that is also applied to the SimpleTemplate property tells ASP.NET that the property’s contents (or template contents) can be placed as an inner property. Hence, we can get a simple mark-up for registering the control on the page.

 

<cc1:SimpleTemplateControl runat=server>
   <SimpleTemplate>
      Hi, I am inside a template. So is the text box:
      <asp:TextBox runat=server />
   </SimpleTemplate>
</cc1:SimpleTemplateControl>

 

This is where templates in server controls become a very familiar scene amongst the creation of composite server controls. Essentially, what the CreateChildControls method does now is tells the template to put the controls into the actual container control (the SimpleTemplateControl control in this case). In the CreateChildControls method, I checked if the template property was null (nothing in Visual Basic) before I called its only member (the InstantiateIn method). The reason for this is to check if a template has been built or not – by a control developer or the ASP.NET parser.

 

However, to explain the logic of the InstantiateIn method: this method is called by the container of the template. If the template has any controls to add to the container control, it has to be done over here and no where else. Basically, the templates in the containing controls are abstracts of controls but are not given life yet because it is not added to the page’s control tree – it is seen as part of the control. Once the InstantiateIn method is called, it is the templates chance to put the abstract controls that it contains into the page’s control tree; hence, giving the controls inside the template, the gift of life.

The parameter that is parsed in the InstantiateIn method is normally the container control which all the abstract controls are added to that control tree. The container control is normally the current control which has the INamingContainer interface applied… but nothing stops the container control to be another control inside the “current” server control (that is when there are “deep” composite controls).


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-2020 ASPAlliance.com  |  Page Processed at 2020-03-29 9:49:07 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search