Click here to Skip to main content
Click here to Skip to main content
Go to top

Dynamically Created Controls in ASP.NET

, 27 Aug 2004
Rate this:
Please Sign up or sign in to vote.
How to dynamically create controls in an ASP.NET page.


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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Leon Langleyben
Web Developer
Israel Israel
Leon works as Chief Architect at SRL Group. He leads architectural design and development of various enterprise level projects.
You can meet him on user groups, conferences and forums dedicated to Architecture, ASP.NET, Team System etc. or join him for the next white water rafting adventure

Comments and Discussions

QuestionValidating and save Dynamically created server side controls data into database Pinmemberankushbelorkar15-Feb-13 21:02 
QuestionAnother link Pinmembersandip_frm_NEPAL22-Jun-12 4:52 
Questiontotally dynamic controls Pinmembermathewtinu25-Apr-12 9:59 
Questionusing vb? PinmemberJavaCookies19-Jan-12 19:24 
AnswerExcellent! Pinmemberhungvm25-Mar-09 23:34 
Questionhow to add events to dynamically created and binding links PinmemberMember 40714403-Mar-09 3:32 
AnswerRe: how to add events to dynamically created and binding links Pinmemberamit_jain_online15-Apr-12 22:15