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.