troubleshooting Question

How to preserve viewstate in my own control during postbacks

Avatar of j_lainio
j_lainioFlag for Finland asked on
5 Comments1 Solution890 ViewsLast Modified:
I have simple custom control which appends one textbox into controlhierarchy. Then I use this control (MyControl) in ASPX page as follows:

    <form runat="server">
      <MyNs:MyControl id="TinyControl" runat="server" />
      <asp:TextBox runat="server" />
      <asp:Button Text="test" runat="server"/>

Works fine, BUT it doesn't persist it's value and when I try to read TextBox value in ASPX codebehind, it's empty!

Question is:
How should I build my class (MyControl) so that TextBox will preserve it's value during postbacks and also its value can be read in ASPX page?

Code for MyControl class:

public class MyControl : System.Web.UI.Control
      TextBox myBox = new TextBox();

      public TextBox MyBox
            get { return myBox;}

      protected override void CreateChildControls()

            MyBox.ID = "SomethingHere";
            MyBox.EnableViewState = true;

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros