在 ASP.NET 中缓存用户控件?

Caching a user control in ASP.NET?(在 ASP.NET 中缓存用户控件?)
本文介绍了在 ASP.NET 中缓存用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序header.ascx"中创建了一个用户控件,我将一个 selectedMenu 属性传递给该控件,该控件在该控件上选择指定的 selectedMenu 值.假设我传递了值home"或search",那么它将选择(突出显示)搜索菜单.

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.

我想缓存这个控件,当 selectedMenu 属性的值发生变化时,只有缓存会被刷新,否则它应该从缓存中获取控件??

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??

是否可以在 asp.net 中缓存用户控件?我正在使用 ASP.NET 2.0 (C#)

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

推荐答案

ASP.NET中的用户控件缓存调用片段缓存.这是通过在页面顶部添加一个 OutputCache 指令来完成的:

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.

判断控件是否应该被缓存的条件是否可以通过查看当前请求来判断?如果是,您可以使用 varybycustom 属性 (http://msdn.microsoft.com/en-us/library/system.web.ui.partialcachingattribute.varybycustom.aspx)的输出缓存指令.您可以将所需的任何字符串作为参数放入其中,然后在评估缓存时 将调用 Global.asxa 中的 GetVaryByCustomString() 方法,您可以将控件是否应缓存的逻辑放在那里.

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.

这篇关于在 ASP.NET 中缓存用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)