German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Creating Templates Programmatically in the Repeater Control

Written by omerkamal on Dec 17, 2006
How to create Repeater Templete with coding behind.

";
}

 

Note   If you have multiple types of controls in your templates, you would need to create a different data-binding event handler for each of the control types.

Introducation:

Some time we need to create a Repeater Templete after accessing the Data from Database or by User Input.To create dynamic templates, you create a template class that you can then instantiate when needed.

Explanation:

To Create a Templete Create a new class that implements the ITemplate interface of the System.Web.UI namespace. This Call will  implement the InstantiateIn method (the only member of the ITemplate interface).  In the InstantiateIn method, create the controls for the template item, set their properties, and then add them to the parent's Controls collection. You can access the parent control via the reference passed to the InstantiateIn method.

 

The following example illustrates a complete template class that displays some static text ("Item number:") and a counter. The counter is maintained as a shared or static value (depending on what language you are using) called itemcount for the class and incremented each time a new item is created.

The class defines an explicit constructor that accepts a ListItemType enumeration value to indicate what type of template is being created. Depending on what type of template is being created, the code creates different types of controls and adds them to the Controls collection of the parent control. The end result is an HTML table with a different background color for the alternating item template.

 

Our ITemplate interface Implementation;

 

public class MyTemplate : ITemplate
{
   static int itemcount = 0;
   ListItemType templateType;

   public MyTemplate(ListItemType type)
   {
      templateType = type;
   }

   public void InstantiateIn(System.Web.UI.Control container)
   {
      Literal lc = new Literal();
      switch( templateType )
      {
         case ListItemType.Header:
            lc.Text = "

";
            break;
         case ListItemType.Item:
            lc.Text = "";
            break;
         case ListItemType.AlternatingItem:
            lc.Text = "";
            break;
         case ListItemType.Footer:
            lc.Text = "
Items
Item number: " + itemcount.ToString() +
               "
Item number: " +
               itemcount.ToString() + "
";
            break;
      }
      container.Controls.Add(lc);
      itemcount += 1;
   }
}

 

In the Page ( .aspx) Our will look like this;

 

private void Page_Load(object sender, System.EventArgs e)
{
   Repeater1.HeaderTemplate = new MyTemplate(ListItemType.Header);
   Repeater1.ItemTemplate = new MyTemplate(ListItemType.Item);
   Repeater1.AlternatingItemTemplate =
      new MyTemplate(ListItemType.AlternatingItem);
   Repeater1.FooterTemplate = new MyTemplate(ListItemType.Footer);
   sqlDataAdapter1.Fill(dsCategories1);
   Repeater1.DataBind();
}

 

Adding Data Binding to Templates;

 

add the data-binding event handler to class:

 

Literal lc = new Literal();
case ListItemType.Item:
   lc.Text = "

";
   lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
   break;

 

Implement the DataBinding EventHandler;

 

private void TemplateControl_DataBinding(object sender, System.EventArgs e)
{
   Literal lc;
   lc = (Literal) sender;
   RepeaterItem container = (RepeaterItem) lc.NamingContainer;
   lc.Text += DataBinder.Eval(container.DataItem, "CategoryName");
   lc.Text += "

Visitors/Readers Comments



"Be the First to Comment!"


Add your Comments

Name:
 
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)