How to mantain user control viewstate on page_load after postback?

Posted on 2009-04-28
Last Modified: 2013-12-17

The piece of code below is to programatically add a user control. The problem that i have is that, on the page_load event, after a postback, the data in the textboxes are GONE.

I do not have this problem if i put the code in the button_click event. The data in the textboxes can be maintained after a postback.

Any help is greatly appreciated.

PS: Unless i entered the data manually on the textboxes with the code in page_load event, then the data is able to be maintained after a postback. Do not want this though.

Dim TextModule As TextModule = LoadControl("~/UserControl/Text.ascx")

        TextModule.ID = "TextModule" & Session("ctrl_id_increment")

        'Add in to page placeholder


        PlaceHolder1.Controls.Add(New LiteralControl("<br />"))

        'Find all the hidden web controls

        Dim txtSequence As TextBox = TextModule.FindControl("txtSequence")

        Dim txtControlID As TextBox = TextModule.FindControl("txtControlID")

        Dim txtType As TextBox = TextModule.FindControl("txtType")

        'Get and assign identifier to each user control

        txtSequence.Text = Session("ctrl_id_increment")

        txtControlID.Text = Session("ctrl_id_increment")

        txtType.Text = "TextModule"

        'Finally add the controls to the string list.

        ctrls.Add(String.Format("{0}|{1}", "TextModule", Session("ctrl_id_increment")))

Open in new window

Question by:tangteng78
    LVL 26

    Assisted Solution

    by:Anurag Thakur
    read this - a very good article for the same issue
    LVL 28

    Accepted Solution

    When working with dynamic controls you need to remember several things:

    1) The control MUST have the exact same ID each time it is created. This allows .Net to associate the viewstate data with the correct control

    2) Setting the value of a control should ONLY occur when !Page.IsPostBack

    3) Controls should be created at Page_Load or earlier ideally.
    protected void Page_Load(object sender, EventArgs e)
        TextBox myTest = new TextBox();
        myText.ID = "MyText";
           myText.Text = "Preset Text";

    Open in new window


    Author Closing Comment

    bulls eye

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    729 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

    23 Experts available now in Live!

    Get 1:1 Help Now