0

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

Gaurav
  • 330
  • 7
  • 21
  • 1
    I actually just discovered that doing what you've done here - deriving from TemplateField and setting the ItemTemplate in the constructor - is the only way to get a TemplateField column to properly persist on postback. I'd have to see the rest of your code to work out why you're losing the checkbox state though, it works for me. – Dylan Nicholson Sep 11 '13 at 02:52

1 Answers1

0

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)

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • Hidden field or viewstate is similar thing. I don't want to do it manually because the problem is not limited to checkbox, I have to use different types of template fields in future which may contain dropdowns etc. – Gaurav May 15 '12 at 10:14