Introduction
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 =
(WebUserControl1)LoadControl("WebUserControl1.ascx");
newUserControl.ID = "newUserControl";
this.PlaceHolder.Add(newUserControl);
this.PlaceHolder.Add(newButton);
}
Problem
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.
Solution
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)
{
RecreatePersistedControls();
}
private void Button_Click(object sender, System.EventArgs e)
{
CreateControl("newControl");
PersistControl("newControl");
}
private void RecreatePersistedControls()
{
}
private void CreateControl(string id)
{
}
private void PersistControl(string id)
{
}
Download source files to see the fully functional sample.
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 VS.net, Visual Basic, VB.net, C#, ASP.net, J# Palm/OS, Java, Websphere, Lotus Notes/Domino, Ajax, Cold Fusion, XML, MS SQL Server, and Oracle.
Microsoft Universal Development Program Partner