Caching a user control in ASP.NET?

后端 未结 4 401
鱼传尺愫
鱼传尺愫 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: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.

提交回复
热议问题