开发者

How Do I Make a TemplateField with Checkbox Through Code-behind?

开发者 https://www.devze.com 2023-02-28 21:45 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消