views:

1639

answers:

3

Previously Called: How to deal with dynamically created controls under load in aspx

in response to a question below: the information required to determine which controls to restore is contained in a dedicated viewstate object.

I am dynamically creating controls in the codebehind page - these controls are all hooked up to click handlers so when a postback occurs I must re-create the previous set of controls, then clear the controls down and generate the new set of controls based on the previous click.

This is coded and working correctly under normal circumstances esentially as follows:

in Page_Load
if not postback generate default buttons 
else if postback re-generate buttons that were shown on last page

in click_handler
Clear the dynamically generated buttons created in the Page_Load
generate new buttons based on the specific click being handled

however when the server comes under load we start getting problems:

With 5 users per second we start getting the exception: Multiple controls with the same ID 'add0' were found. FindControl requires that controls have unique IDs.

With 100 users per second we start getting the exception: The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.

Once this occurs all subsequent requests get the same error and IIS has to be re-started.

What could be causeing this and how can I avoid it? Do html requests possibly overwrite and interfere with each other when under load? do objects somehow hand around after a page unload in a manner that would allow the next page load to trip over them?

+2  A: 

How are you storing information about the controls you need to restore? If you are using ViewState or ControlState, then I don't see how load could affect things. That's how any of the composite controls do things.

I will say that I saw your second error while using the Infragistics UltraWebGrid, and never was able to track it down. From the call stack, it appeared that EnsureChildControls was being called during the Load phase (or maybe LoadViewState).

John Saunders
A: 

Everything you have written seems to be correct and doable. Most likely this is an issue with your control generation code. Perhaps if you post some of that we can better find a solution.

Daniel
+2  A: 

A private static variable was being used to store a dictionary of names and table cells so that table cells would not get re-created during the page lifecycle.

The key point is that it was marked static - it should have been an instance variable - the end result being that under load when requests started backing up then multpile requests were sharing this static dictionary.

exactly what happened i'm not 100% sure - but under medium loads FindControl would find multiple controls of the same name, under very high loads it seems one request would try to modify a control (probably add to it) while it was in an invalid state from the other request.

End result - if you dont really know what your doing - prefer instance variable sto static variables.

dice