I am new to ASP.NET programming.
I would like to add controls to my current page when a button is clicked.
Currently, this is the event that gets fired:
protected void Button_Click(object sender, EventArgs e) {
LiteralControl litctrl = new LiteralControl("<div> Testing " + DateTime.UtcNow.Millisecond +" </div>");
form.Controls.Add(litctrl);
}
This works fine on the first click but if I click again, I do not get an extra div added to the form. A new div is created but the previous one is lost.
Why is it so and how can I create controls additively on the current page ?
See http://chiragrdarji.wordpress.com/2009/05/20/maintain-viewstate-for-dynamic-controls-across-the-postback/ for some examples. Adding dynamic controls in ASP.NET Webforms is a tricky dance between understanding postbacks and the page lifecycle. Throw in any ViewState requirements and it takes some even deeper understanding.
For your requirements, you need to recreate any dynamically added controls on every postback. As you aren’t, it disappears.