In some cases you may end up with the following exception when working with RadControls for ASP.NET Ajax:
"Script controls may not be registered after PreRender."
The explanation of this error is simple - some UI control implementing the IScriptControl interface (e.g. any control from the RadControls for ASP.NET Ajax suite) is stored in the Session, Application or Cache and then is added in some live controls collection. Here is an example:
Here a Timer control is stored in session state thus avoiding its creation on every page load. Sounds quite reasonable, doesn't it? Unfortunately not. If you load this page and then press the "Postback" button you will end up with the yellow screen of death saying that "Script controls may not be registered after PreRender". But why? The answer is simple - caching. The Timer control (as well as most IScriptControls) caches a reference to the current ScriptManager (current means "from the current Page"). Here is how:
During the first load of the sample page everything is fine. The ScriptManager property is properly initialized and the page runs happily. After postback the Timer control is pulled out from session state. As caching is implemented the _scriptManager field will not be null and the Timer control will try to register with an old ScriptManager. However the life cycle of the page object, to which that "old" ScriptManager belonged, has been executed during the previous request(including the PreRender event). When the Timer control tries to register with the ScriptManager we end up with the exception that the PreRender method has already been executed - and it really was during the first request to the page.
Although this exception is specific to ASP.NET Ajax enabled controls you should avoid caching *any* ASP.NET controls at all costs. Tess Ferrandez from Microsoft has a series of blog posts telling why storing UI controls is not a good idea:
But if this is not a proper way of caching ASP.NET UI controls what is? Cache the data used to populate the control instead. Or use output caching.
I hope this helps,
Subscribe to be the first to get our expert-written articles and tutorials for developers!