Click here to Skip to main content
14,880,797 members
Articles / Web Development / ASP.NET
Posted 19 Feb 2006


27 bookmarked

Accessing Data From Dynamically Created Controls Using ASP.NET 2.0 Callback

Rate me:
Please Sign up or sign in to vote.
3.44/5 (8 votes)
19 Feb 20061 min read
Storing and retreiving data input from dynamically created controls without the need to recreate the controls after postback.


Adding controls to an ASP.NET WebForm at runtime is a simple task:

private void Page_Load(object sender, System.EventArgs e)
    Button newButton = new Button();
    newButton.Text = "New Button"; 
    newButton.ID = "newButton"; 
    newButton.Click += new System.EventHandler(this.newButton_Click); 

    WebUserControl1 newUserControl = 
    newUserControl.ID = "newUserControl";


Now, let's raise a bit little. What if we want to add a new control as a result of some user action (button click, for example)? We are moving the same code from Page_Load to Button_Click event and… here troubles begin. Our controls will show as expected, but only the first time. Any postback will reset the page to its original state. Dynamically created controls will be unable to fire any event. What is happening here? The answer is simple. Page class is stateless. It is destroyed after rendering. Page recreates child controls collection, based on tags in aspx file, then postback data can be handled and event handlers attached (in OnInit event). Controls we just created dynamically do not exist in aspx file; Page has no knowledge about them.


The solution is also simple. We have to recreate controls on load stage of page lifecycle. After it's done, each control can handle its postback data, retrieve saved viewstate, raise events etc.

Now, the code skeleton will look like this:

private void Page_Load(object sender, System.EventArgs e)
private void Button_Click(object sender, System.EventArgs e)
private void RecreatePersistedControls()
    // Call CreateControl for each persisted control
private void CreateControl(string id)
    // Create controll with specified id, 
    // add it to controls collection, attach event handlers
private void PersistControl(string id)
    // Persist specified id (in session for example)

Download source files to see the fully functional sample.


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

Web Developer
United States United States
Possess over 17 years of experience and expertise in the IT industry. Possess in-depth knowledge and experience in the areas of database development, modeling, implementation, wireless integration, and migration, extensive web development skills, and several years of experience providing consulting and training services. Extensive work experience on several integration project for Dell Corporation, Fujitsu, Scholle, Sperry Marine, Vought Aircraft to name a few. Complete understanding of the importance of meeting timelines and budgetary restraints on a per project basis.

As a key member of Brady Systems Integration, created solutions that automated data collection from manufacturing and warehouse thru wireless devices like Symbol, Intermec, and HHP and integrated that data and transactions into the clients back-end business systems. Possess experience integrating into Baan, SAP, JD Edwards, and Great Plaines ERP Systems.

Technical skills include, Visual Basic,, C#,, J# Palm/OS, Java, Websphere, Lotus Notes/Domino, Ajax, Cold Fusion, XML, MS SQL Server, and Oracle.

Microsoft Universal Development Program Partner

Comments and Discussions

GeneralCode Doesn't Work Pin
maineiac4518-Nov-10 7:00
Membermaineiac4518-Nov-10 7:00 
QuestionCan find dynamic added Control in system.Windows? Pin
Rajeesrivastava8-Jan-10 19:12
MemberRajeesrivastava8-Jan-10 19:12 
GeneralSource Code Not Working Pin
bond_with_best17-Nov-08 18:38
Memberbond_with_best17-Nov-08 18:38 
GeneralRun Time Control Creation Pin
Jitku28-Sep-06 3:33
MemberJitku28-Sep-06 3:33 
GeneralRe: Run Time Control Creation Pin
Navneet Khehra6-Aug-07 2:06
MemberNavneet Khehra6-Aug-07 2:06 
GeneralDoesn't work for dropdowns Pin
GeorgeMar21-Feb-06 3:49
MemberGeorgeMar21-Feb-06 3:49 
GeneralRe: Doesn't work for dropdowns Pin
vineyard21-Feb-06 4:19
Membervineyard21-Feb-06 4:19 
GeneralRe: Doesn't work for dropdowns Pin
GeorgeMar21-Feb-06 4:26
MemberGeorgeMar21-Feb-06 4:26 
GeneralRe: Doesn't work for dropdowns Pin
vineyard21-Feb-06 4:45
Membervineyard21-Feb-06 4:45 
GeneralRe: Doesn't work for dropdowns Pin
simone massaro21-Feb-06 9:46
Membersimone massaro21-Feb-06 9:46 
GeneralRe: Doesn't work for dropdowns Pin
GeorgeMar22-Feb-06 4:13
MemberGeorgeMar22-Feb-06 4:13 
GeneralRe: Doesn't work for dropdowns Pin
GeorgeMar22-Feb-06 4:14
MemberGeorgeMar22-Feb-06 4:14 
GeneralRe: Doesn't work for dropdowns Pin
vineyard22-Feb-06 4:28
Membervineyard22-Feb-06 4:28 
GeneralRe: Doesn't work for dropdowns Pin
thisguyinoc14-Jun-06 8:15
Memberthisguyinoc14-Jun-06 8:15 
GeneralRe: Doesn't work for dropdowns Pin
Ankit M1-Mar-07 7:43
MemberAnkit M1-Mar-07 7:43 

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.