Question

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?

Was it helpful?

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

OTHER TIPS

Since a Control is specific to a Page (e.g. has a Page property), you can't store it in Cache and share it between multiple pages. Maybe you might look at fragment caching instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top