Rank: Member Groups: Member
Joined: 7/7/2007 Posts: 19
|
I need to cache a collection of menuitems because several DB calls are required to properly configure my menuitems and my menu needs to persist between multiple pages. I thought I would be able to easily do this by storing a MenuItemCollection object in the session cache and assigning it to the Menu. However I've run into two problems in doing this:
1. MenuItemCollection has no constructor.... so how do I instantiate a new instance of it to populate it with menuitems?
2. Even if I could create a new MenuItemCollection, I don't see any way to assign a MenuItemCollection to the menu control. The Items property is read-only.
Do you have an example anywhere of caching the menuitems so I don't need to regenerate them every time?
Thanks,
Brad
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Hi Brad,
You should never cache MenuItem or MenuGroup object. These objects, like the control itself, participate in the page's life cycle and should only be used by the page once. Once rendered, these objects became "dirty" and can not be used again.
The menu automatically keeps the whole menu structure in viewstate within the same page. If you need to keep the menu structure between different pages, you would need to keep a copy of your data from which the menu is built. For example, if you create the menu from a DataSet object, you would need to keep that DataSet object and recreate the menu from that DataSet object every time the page is requested.
Hope this helps.
Thanks
|
Rank: Member Groups: Member
Joined: 7/7/2007 Posts: 19
|
It does help. Thanks!
My menu structure and whether certain elements of the menu are visible or not are determined by a stored procedure that returns this information. This stored procedure must check multiple tables, so I was trying to avoid calling this stored procedure for every page load. I guess I'll just have to cache the information that this stored procedure returns and regenerate the menu every time based on that.
Thanks,
Brad
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Quote:I guess I'll just have to cache the information that this stored procedure returns and regenerate the menu every time based on that.
Yes. I think that's the correct way to do it.
|