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

Modifying Templates Programmatically (Advanced)

Now that we got the basics of programmatically changing the template, let’s do a rather simple approach for creating a programmatic template for the Repeater control. First, I am going to build the template class and explain the new things that made it through into this example:

 

public class CustomItemTemplate : ITemplate {
   private CustomRepeater repeater;
 
   public void InstantiateIn(Control container) {
      Label label = new Label();
      label.DataBinding += new EventHandler(LabelDataBounded);
      container.Controls.Add(label);
      container.Controls.Add(new LiteralControl("<br>"));
   }
 
   public CustomItemTemplate(CustomRepeater parentRepeater) {
      repeater = parentRepeater;
   }
 
   private void LabelDataBounded(object sender, EventArgs e) {
      Label label = (Label)sender;
 
      if ((repeater.DataItemText != null) && (repeater.DataItemText.Length != 0))
      label.Text = DataBinder.Eval(((RepeaterItem)label.NamingContainer).DataItem,
         repeater.DataItemText).ToString();
   }
}

 

On the constructor, the parameter that is parsed is to reference of the containing repeater. This will be used when the item is being data bounded (will continue later part on this page).

 

From there on, the explanation is as per normal template (covered on previous page). The only difference is that there is a control called CustomRepeater. It is quite obvious it is a custom control that derives from the Repeater control:

 

public class CustomRepeater : Repeater {
   private string pDataItemText;
 
   public string DataItemText {
      get { return pDataItemText; }
      set { pDataItemText = value; }
   }
 
   public CustomRepeater() {
      ItemTemplate = new CustomItemTemplate(this);
   }
}

 

An additional property was added and named DataItemText. This is where the reference to the CustomRepeater, in the CustomItemTemplate constructor, is vital. The property will basically record the page developer’s instruction of which field the CustomItemTemplate will use to fill the Label control that it withholds.

 

When the CustomRepeater is data-bounded, this is also the point where the Label gets data bounded too. In turn, the DataBinding event is raised on the Label control because the RepeaterItem (which will contain the template instance) is data-bounded. Internally, the RepeaterItem tells the templates to instantiate its controls into the RepeaterItem; and then the RepeaterItem tells all children controls (that was added from the template) to data bind by calling the DataBind method.

 

The DataBind method raises the DataBinding event. The sender of this event is the control that its DataBind method was called. With that said, the delegated method, the LabelDataBounded method, the sender of the control is actually the Label.

 

The LabelDataBounded method then checks if the DataItemText property, from the CustomRepeater, is not null (Nothing in Visual Basic). If it is not null, it then uses the trusty DataBinder.Eval method. The only difference over here is that the first parameter is not the normal “Container”. Instead, the first parameter points to the RepeaterItem control which contains the DataItem that the current “row” of data must bound to.

 

And the importance of the reference to the CustomRepeater is to get the column which the Label will output.

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