How can I add a template field with a checkbox to my DetailsView object through C# code-behind? I'm having trouble figuring it out from reading the asp.net code.
I already have the TemplateField and CheckBox object开发者_JS百科 instantiated with values assigned to the properties. But when I use Fields.Add() the checkbox doesn't show-up.
TemplateField tf_ForMalls = new TemplateField();
tf_ForMalls.HeaderText = "For Malls";
CheckBox chk_ForMalls = new CheckBox();
chk_ForMalls.ID = "chkDelete";
tf_ForMalls.ItemTemplate = chk_ForMalls as ITemplate;
dv_SpotDetail.Fields.Add(tf_ForMalls);
You will need a custom class derived from ITemplate to get this working
public class MyTemplate : ITemplate
{
#region ITemplate Members
public void InstantiateIn(Control container)
{
CheckBox chk = new CheckBox();
chk.ID = "chk";
container.Controls.Add(chk);
}
#endregion
}
Then in the code
TemplateField tf = new TemplateField();
tf.ItemTemplate = new MyTemplate();
detvw.Fields.Add(tf);
You can have the constructor to pass in the parameters for 'control id' or specifying the ListItemType
Hope this helps
精彩评论