Click here to Skip to main content
13,404,383 members (44,769 online)
Click here to Skip to main content
Add your own
alternative version


91 bookmarked
Posted 18 Feb 2003

Retaining State for Dynamically Created Controls in ASP.NET applications

, 18 Feb 2003
Rate this:
Please Sign up or sign in to vote.
When creating controls on the fly on your ASP.NET pages viewstate is a bit hard to track. Learn how it works and how to help your application remembers where to put what.


When dynamically adding controls to an ASP.NET page in runtime the object references are lost at postback because they have no handle in the codebehind. The values entered by the user is not availible when accessing these objects after postback, and they seem empty. This article describes how to use ViewState to recreate and reinsert the postback values into the objects to access their values.


An ASP.NET application with pages that use dynamic generation of controls that need to retain state across postbacks. The solution described in this paper enables you to create a totally dynamic number of controls at runtime and retreive their values after postback.


Dynamically added controls have no object reference variable in the codebehind class. They appear only in the control collection of the containing control, i.e. the Page.Controls collection. When the page is posted back to the server as a result of user interaction a new instance of the codebehind class is instantiated, and all the variables of the class is set with values from the ViewState.

This means that the objects we are accessing from the codebehind class, that "feels" like the same objects as we worked on before postback, actually are new ones that got their predecessors values via ViewState and ASP.NET state management.

So, the controls that were dynamically created are no longer there and consequently the values returned from these controls have no place to go. They are lost in the viewstate.

In order to catch these values the dynamically generated controls needs to be re-generated at Page_Load. The important thing is to assign the same ID to each control. The ViewState uses the ID property of the Control objects to reinstate the values.

Add the following elements to your System.UI.Page class:

public class DynamicallyAddingControls : System.Web.UI.Page
    // a Property that manages a counter stored in ViewState
    protected int NumberOfControls
        get{return (int)ViewState["NumControls"];}
        set{ViewState["NumControls"] = value;}

    private void Page_Load(object sender, System.EventArgs e)
            //Initiate the counter of dynamically added controls
            this.NumberOfControls = 0;                
            //Controls must be repeatedly be created on postback

    // This routine creates the controls and assigns a generic ID
    private void createControls()
        int count = this.NumberOfControls;

        for(int i = 0; i < count; i++)
            TextBox tx = new TextBox();
            tx.ID = "ControlID_" + i.ToString();
            //Add the Controls to the container of your choice

    // example of dynamic addition of controls
    // note the use of the ViewState variable
    private void addSomeControl()
        TextBox tx = new TextBox();
        tx.ID = "ControlID_" + NumberOfControls.ToString();



Note that the createControls method has to simulate the way that you dynamically built your page before the postback. The important thing here is obviously to assign identical ID values to the correct type of controls to that we can access them at postback.

Points of Interest

This solution works because the ASP.NET ViewState supports dynamically added controls, but does not do the re-generation for us. As long as the ID properties match ASP.NET does the rest of the job for us.

Thanks to Eric Sütter for providing valuable input on this article!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Mads Nissen
Software Developer (Senior)
Norway Norway

You may also be interested in...


Comments and Discussions

GeneralRe: Label problem Pin
smair30-Aug-05 23:20
membersmair30-Aug-05 23:20 
GeneralMaintain state with linkbutton Pin
kannadasan9-Sep-04 23:54
memberkannadasan9-Sep-04 23:54 
GeneralFree Treeview control for ASP.Net Pin
Anonymous26-Feb-04 21:41
sussAnonymous26-Feb-04 21:41 
GeneralEvents Pin
loginbajada16-Oct-03 4:09
sussloginbajada16-Oct-03 4:09 
GeneralEvents Pin
loginbajada16-Oct-03 4:08
memberloginbajada16-Oct-03 4:08 
Generalregarding https Pin
jagadeesh12310-Sep-03 1:01
memberjagadeesh12310-Sep-03 1:01 
GeneralHELP with dynamic controls Pin
Anonymous26-Aug-03 10:08
sussAnonymous26-Aug-03 10:08 
GeneralRe: HELP with dynamic controls Pin
Anonymous26-Aug-03 13:40
sussAnonymous26-Aug-03 13:40 
GeneralRe: HELP with dynamic controls Pin
Anonymous27-Aug-03 12:09
sussAnonymous27-Aug-03 12:09 
GeneralRe: HELP with dynamic controls Pin
Litera17-Jun-04 22:33
memberLitera17-Jun-04 22:33 
GeneralRe: HELP with dynamic controls Pin
TomDickAndHarry28-Jun-05 12:40
sussTomDickAndHarry28-Jun-05 12:40 
GeneralRe: HELP with dynamic controls Pin
beh128-Dec-05 14:56
memberbeh128-Dec-05 14:56 
GeneralRe: HELP with dynamic controls Pin
parkly16-Aug-07 7:06
memberparkly16-Aug-07 7:06 
GeneralRe: HELP with dynamic controls Pin
aaraaayen30-Aug-06 3:08
memberaaraaayen30-Aug-06 3:08 
GeneralBetter solutions than my original Article! Pin
Mads Nissen6-Aug-03 3:02
memberMads Nissen6-Aug-03 3:02 
GeneralDynamicControlsPlaceholder Pin
Jeffry van de Vuurst6-Aug-03 2:29
memberJeffry van de Vuurst6-Aug-03 2:29 
QuestionDatagrid? Pin
jpfouche27-Jun-03 6:01
memberjpfouche27-Jun-03 6:01 
AnswerRe: Datagrid? Pin
empire2924-Mar-06 11:44
memberempire2924-Mar-06 11:44 
QuestionDo this with a DataGrid with TextBox ItemTemplates? Pin
jpfouche27-Jun-03 3:10
memberjpfouche27-Jun-03 3:10 
QuestionWhy Store your counter in Special Page Class? Pin
jpfouche27-Jun-03 1:57
memberjpfouche27-Jun-03 1:57 
GeneralKind of convoluted Pin
Mike Newman27-Feb-03 16:26
sussMike Newman27-Feb-03 16:26 
GeneralControl.CreateChildControls Pin
leppie19-Feb-03 7:23
memberleppie19-Feb-03 7:23 
GeneralRe: Control.CreateChildControls Pin
Helderix24-Nov-06 2:56
memberHelderix24-Nov-06 2:56 
GeneralPage_Init instead Pin
LukeDuff19-Feb-03 7:02
memberLukeDuff19-Feb-03 7:02 
GeneralRe: Page_Init instead Pin
Anonymous19-Mar-03 1:26
sussAnonymous19-Mar-03 1:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1802120.2 | Last Updated 19 Feb 2003
Article Copyright 2003 by Mads Nissen
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid