We help IT Professionals succeed at work.

How to persist child controls in custom control?

bemara57 asked
Medium Priority
Last Modified: 2013-11-07
I am adding checkboxes to my custom control and expose them through properties. How can I persist these children controls? I get an error saying the CheckBox controls are not serializable so I cannot save them to the viewstate. My code that I am trying is below. How can I persist the checkboxes to the custom control- am I not exposing these child controls properly?
public class CustomControl : Control
public CheckBox Chk1
   get {
      if (ViewState["Chk1"] == null)
         CheckBox control = new CheckBox();
         control.ID = "chk1";
         control.Text = "Test 1";
         ViewState["Chk1"] = control;
      return ViewState["Chk1"] as CheckBox;
   set { ViewState["Chk1"] = value; }
public CheckBox Chk2
   get {
      if (ViewState["Chk2"] == null)
         CheckBox control = new CheckBox();
         control.ID = "chk2";
         control.Text = "Test 2";
         ViewState["Chk2"] = control;
      return ViewState["Chk2"] as CheckBox;
   set { ViewState["Chk2"] = value; }
protected override void CreateChildControls ()
   Controls.Add (this.Chk1);
   Controls.Add (this.Chk2);

Open in new window

Watch Question

You can keep them in your session, I have an example here:

With the code here:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.