Reputation: 1448
I have a page with a list of lot of Products from the database. And on that same page I have some info of the logged in user. This info is in a UserControl. (It is not possible to put the list with products in a UserControl :))
So I set this at the top of my page to cache the page
<%@ OutputCache Duration="200"
Location="Any"
VaryByParam="none"
%>
But because this caches the whole page, the UserControl with the user info is also cached. Is it possible to disable the caching for only the UserControl, but not for the rest of the page?
I had a look at Substitution Blocks. But it seems that this only works with text?
Thanks,
Vincent
Upvotes: 1
Views: 1555
Reputation: 32333
From MSDN:
To allow you to cache a page but substitute some content dynamically, you can use ASP.NET post-cache substitution. With post-cache substitution, the entire page is output cached with specific parts marked as exempt from caching. In the example of the ad banners, the AdRotator control allows you to take advantage of post-cache substitution so that ads dynamically created for each user and for each page refresh.
There are three ways to implement post-cache substitution:
Declaratively, using the Substitution control.
Programmatically, using the Substitution control API.
Implicitly, using the AdRotator control.
I think the best option would be using of Substitution
control. For this add a Substitution
control to your page, and set its MethodName
property:
<asp:Substitution runat="server" MethodName="GetUserInfo"></asp:substitution>
Now add GetUserInfo
method to your page. The Substitution
control calls this method to retrieve the user info:
public static string GetUserInfo(HttpContext context)
{
// return rendered user control
}
The thing left is to render your user control into a string. For this you could use a technique proposed in the Tip/Trick: Cool UI Templating Technique to use with ASP.NET AJAX for non-UpdatePanel scenarios article by Scott Guthrie. In this case to render the user control just use something like:
return ViewManager.RenderView("UserInfo.ascx");
Upvotes: 4
Reputation: 10105
Place the product list in a Session
. Now access it anywhere from the application.
YourColectionClass variable = Session["Sessionvariable"] == null ?
ClassObject.DatabaseFunction(Params) :
(YourColectionClass)Session["Sessionvariable"];
Now Cache
it on the basis of VaryByparam
. VaryByParam
can have UserID.
Upvotes: 0