State of checkbox lost after postback in a custom template field

1k views Asked by At

I have created a custom checkbox template field by deriving it from System.Web.UI.WebControls.TemplateField. The template for this field has been created by creating a class which implements ITemplate interface. When any postback happens on the page the values in the checkboxes is lost.

To get this working temporarily I have used viewstate to store the state of checkboxes in the checkbox column, but going further I want to completely avoid this as I will be using more template fields in same fashion in my application.

Please let me know if I am missing anything.

Following is the code:

namespace MyControls
{
    public class CheckBoxTemplateField : TemplateField
    {
        public CheckBoxTemplateField()
        {
            this.HeaderTemplate = new CheckBoxTemplate();
            this.ItemTemplate = new CheckBoxTemplate();
        }
    }

    public class CheckBoxTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            CheckBox chk = new CheckBox();
            container.Controls.Add(chk);
        }
    }
}

Regards, Gaurav

1

There are 1 answers

1
Royi Namir On

checkbox is known with their problem maintaining their value in postback

1 solution is to store its value in hidden fiesld and to read it in server.

p.s. thhis has nothing to do with viewstate.input control doesnt saves their value in viewstate ( excpet textbox which has the 'ontextchange' event)