I recently had to solve this problem to allow professors to upload any number of files to our library server using a web form. It seemed simple enough to solve, but as I searched around the net, it seemed many people were having a lot of trouble with this topic or were using some pretty hackish things to get it working. I hope this post helps someone out there.
The main problem of adding new controls to a page is that the new controls will disappear whenever there is a new postback. To solve this problem we simply store the control in the users Session which allows them to keep their data as long as their session stays alive.
We can store the controls in a List to keep track of them. When the page is first created, we store the list inside of the Session, then when the page is loaded, we get the list from the Session. Getting the list from the session when the page is loaded guarantees that we have the list of controls for the current user.
Adding Controls Back to Page After Postback
Whenever a postback occurs, all of the controls on a page are recreated. Since a control spawned within the page cycle is not bound to anything, it is simply destroyed.
In order for the dynamically added controls to show up, they must be added at the beginning of each page in the Page_Load method. In this example, we simply iterate through the list and add the control to a placeholder.
Adding a New Control
Whenever a new control is added inside of a button click method, it will not show up on the page immediately. What happens is that it is actually created and added to the array, but the page cycle is already past the Page_Load method, so there is no chance to add the control to the page from the array. To counter this, we simply have to add the control to the page manually inside of the method. In the next postback, the control will be added automatically like the others.
Unfortunately, I cannot host files on Blogger so I will copy the contents of the files directly to this post. These files were tested with ASP.Net 3.5 and Visual Studio 2008, but they are simple enough to work on pretty much any platform.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<title>Adding Dynamic Controls to a Form</title>
<form id="form1" runat="server">
<asp:PlaceHolder ID="myPlaceholder" runat="server"></asp:PlaceHolder>
<asp:Button ID="addControl" runat="server" Text="Button"
public partial class _Default : System.Web.UI.Page
List<Control> controls = new List<Control>();
protected void Page_Load(object sender, EventArgs e)
if (Page.IsPostBack == false)
Session["controls"] = controls;
controls = (List<Control>)Session["controls"];
/* The controls must be added back to the page each time there
* is a postback, otherwise the controls disappear.*/
foreach (Control control in controls)
protected void addControl_Click(object sender, EventArgs e)
TextBox box = new TextBox();
box.ID = "box" + controls.Count;
/* We add the box manually first since the Page_Load
method has already finished. The box will only show up
after the next postback otherwise.*/