How to persist child controls in custom control?

Posted on 2009-02-17
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

Question by:bemara57
    1 Comment
    LVL 21

    Accepted Solution

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

    With the code here:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now