The
ItemTemplate
needs to be an instance of a class which implements
the ITemplate
interface[
^],
NOT a control instance. It will need to create a new instance of the control for each data item in the parent control.
public class CheckBoxTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
CheckBox child = new CheckBox();
child.DataBinding += BindData;
container.Controls.Add(child);
}
private void BindData(object sender, EventArgs e)
{
CheckBox child = (CheckBox)sender;
IDataItemContainer container = (IDataItemContainer)child.NamingContainer;
child.Checked = DataBinder.Eval(container.DataItem, "Your Property Name Here");
}
}
...
TemplateField tf = new TemplateField();
tf.ItemTemplate = new CheckBoxTemplate();
gridView1.Columns.Add(tf);
ITemplate.InstantiateIn(Control) Method (System.Web.UI) | Microsoft Docs[
^]