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 = "<TABLE
border=1><TR><TH>Items</TH></TR>";
break;
case ListItemType.Item:
lc.Text = "<TR><TD>Item
number: " + itemcount.ToString() +
"</TD></TR>";
break;
case ListItemType.AlternatingItem:
lc.Text = "<TR><TD
bgcolor=lightblue>Item number: " +
itemcount.ToString() + "</TD></TR>";
break;
case ListItemType.Footer:
lc.Text = "</TABLE>";
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 = "<TR><TD>";
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 += "</TD></TR>";
}
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.
|