Blog Categories

Subscribe to RSS feed


ASP.NET User Controls: Dynamically created and hosted in Web Part.

Dynamic controls & Post back Problem: Managing an array of Controls.


When you create an array of controls dynamically there are chances that you would notice the following behavior:

1. When you click on one of the controls a post back will be triggered for some other control in the array of controls.

2. When you click on the same control again then the post back will occur for the selected/clicked control.


When a web request is received, a series of objects are created by the ASP.NET framework. The control hierarchy is again built up from scratch. Each control is assigned a unique client ID by the framework. Since each click on the control will trigger a post back (Request), there is a possibility that the each control in the array of controls will get a different Client ID assigned to them when they are rendered. This implies that the control’s client ID in the post back request might actually be assigned to some other control and thus invoking a different event handler or executing a different path of code.


The solution is simple, to pin down the Client ID of the controls while creating them. Simply assign a unique client ID to each of the controls in the array while creating them.

For example:

for(int iControlIndex =0; iControlIndex < iArrayLength; iControlIndex++)


oControlArray[iControlIndex] = new ….

oControlArray[iControlIndex].ClientID = “YourUniqueName_” + iControlIndex;


Leave a Reply