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.*/
Thanks!!!!!......Was very Helpful
Thanks!! soooo helpful
I want to add more than one field at a time (5 fields actually) but I don't want them all in a line.. how do I do that?
You could add line breaks between them by using the HTML br tag.
controls.Add(new LiteralControl("<br />"));
myPlaceholder.Controls.Add(new LiteralControl("<br />"));
Thanks a lot!!!
I've already spent a lot of time to make a dynamic array of controls before)))
Thanks i got a good help from this article and i want to know the method to get values from multiple textboxes. Please help
Thanks! This was an incredible amount of help to me.
Playtech Casino Software - JT Hub
Playtech Casino 과천 출장샵 Software · Playtech. Casino Software 보령 출장안마 · Playtech's HTML5 Games. 나주 출장안마 Software and software developer · Playtech's 고양 출장마사지 HTML5 Games. 정읍 출장마사지 Software · Playtech's HTML5 Games.
Post a Comment