Caching a user control in ASP.NET?

后端 未结 4 400
鱼传尺愫
鱼传尺愫 2021-01-18 02:43

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 spe

相关标签:
4条回答
  • 2021-01-18 03:06

    To summarize

    using VaryByCustom, means

    1- Build the control again.
    2- Having multiple versions of the control in the cache. which will be used when the custom conditions meet.

    This is only good if the control is taking a lot of time to build and we have finite number of cached versions to not waste memory, and the application will need to access control properties (while it is cached "or null").

    but it will not be good if that custom conditions are depending on the control properties itself. I can't access it, it is null.

    for example I want to write something like if (the default selected value in countries list is NOT USA) then rebuild and Cache (give it a different string)

    Otherwise don't

    while other objects are trying to access the contries list, it will check for null, and set the countries drop down list to USA.

    The data cahing will do the work. it is the only way.

    who agree?

    Thanks for ur time

    0 讨论(0)
  • 2021-01-18 03:10

    I don't think it's a good idea to cache the control itself:

    • When the control is created the very first time, it has references to its parent page among others.
    • When you retrieve the control from the cache, those references does no longer exists.

    A better approach, I think, is to cache the data which the control is using instead. ASP.NET creates so many controls during the page life cycle, that caching this one control really doesn't improve anything.

    Then a stupid question at the end: Is this control a bottleneck? Do you really need the cache?

    0 讨论(0)
  • 2021-01-18 03:12

    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.

    0 讨论(0)
  • 2021-01-18 03:16

    Of course you can! It's called "Fragment Caching". Here's a link to the Quickstarts page and the MS Knowledge base. Additionally, Google.

    0 讨论(0)
提交回复
热议问题