Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching a user control in ASP.NET?

I have created a user control in my application "header.ascx", I am pasing a selectedMenu attribute to this control on which the control selects the selectedMenu value specified. Suppose, I have passed value "home" or "search" then it will select (highlight it) the search menu.

I want to cache this control, When the value of the selectedMenu attribute changes then only the cache will be refreshed else it should picks up the control from cache??

Is it possible to cache a user control in asp.net?? I am using ASP.NET 2.0 (C#)

like image 329
djmzfKnm Avatar asked Feb 20 '09 09:02

djmzfKnm


People also ask

What are the two types of caching?

L1 cache, or primary cache, is extremely fast but relatively small, and is usually embedded in the processor chip as CPU cache. L2 cache, or secondary cache, is often more capacious than L1.

What is user control in ASP.NET with example?

The following example shows an ASP.NET Web page that contains a user control. The user control is in the file Spinner. ascx in the Controls folder. In the page, the control is registered to use the prefix uc and the tag name Spinner. The user control properties MinValue and MaxValue are set declaratively.

What is caching in ASP.NET MVC?

ASP.NET Core API. Caching means to store something in memory that is being used frequently to provide better performance. We will see how you can dramatically improve the performance of an ASP.NET MVC application by taking advantage of the output cache.


1 Answers

User control caching in ASP.NET is called fragment caching. It's done by adding an OutputCache directive to the top of your page:

You can't vary the cache by setting the property on the control because the control isn't actually created if it's found in the cache. If you try to access the control in the code behind it's cached, it will be null.

Is the condition that determines whether the control should be cached or not something that you can determine by looking at the current request? If it is, you can use the varybycustom attribute (http://msdn.microsoft.com/en-us/library/system.web.ui.partialcachingattribute.varybycustom.aspx) of the output cache directive. You can put any string you want in there as the parameter and then when the caching is evaluated the GetVaryByCustomString() method from Global.asxa will be called and you can put the logic for whether the control should be cached or not there.

like image 200
Helephant Avatar answered Sep 26 '22 20:09

Helephant