Search code examples
asp.netweb-controls

Asp.net Is it possible to store server controls in the cache?


Asp.net Is it possible/bad idea to store server controls in the cache?

Is it a bad idea to create a control ie: HtmlGenericControl and then add it to the Page.Cache and then retrieve it on OnInit() and add it to the pages controls collection?

Or is it better to use ParseControl?


Solution

  • It is rather dangerous. Every time the page is invoked you get an instance of it. This instance will have the instances of all controls the designer put in. If you store the instance of the control in the cache you may get into a situation where multiple instances of a page will share the instance of the control. You need to be careful what goes into this control data portion because it will be also shared