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

Modifying Templates Programmatically (Simple)

I am going to demonstrate the basics of setting templates programmatically. To do a basic demonstration, I will use the simple template example that was demonstrated at the beginning of the article. But first, let’s create the template class:

 

public class SimpleCustomTemplate : ITemplate {
   public void InstantiateIn(Control container) {
      container.Controls.Add(new LiteralControl(“Hello World from the template”));
   }
}

 

Is it simple? To do a follow through, the InstantiateIn method is called. It then adds a literal control to the container control which is parsed by the only parameter. Now to hook it up with the control provided earlier in the article:

 

public class ExtendedTemplateControl : SimpleTemplateControl {
   public ExtendedTemplateControl() {
      SimpleTemplate = new SimpleCustomTemplate();
   }
}

 

The code is really simple: all it does is set the SimpleTemplate property to the template that was defined earlier on this page (the SimpleCustomTemplate class). This, however, allows the page developer to override our template by the usual course. If you want to force your custom template, then do it at the last minute – at the CreateChildControls method:

 

public class ExtendedForcedTemplateControl : SimpleTemplateControl {
   public overrides void CreateChildControls() {
      SimpleTemplate = new SimpleCustomTemplate();
      base.CreateChildControls();
   }
}

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-04-25 7:29:17 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search