通过 Propety 缓存 Web UserControl 不起作用(糟糕!)

发布于 2024-08-31 23:00:03 字数 1053 浏览 7 评论 0原文

这是我的控件背后的代码:

<PartialCaching(60, Nothing, "UsrCtl_WebUserControl.CacheString", Nothing, True)> _
Partial Class UsrCtl_WebUserControl
 Inherits System.Web.UI.UserControl

 Private _CacheString As String

 Public Property CacheString() As String
  Get
   Return _CacheString
  End Get
  Set(ByVal value As String)
   _CacheString = value
  End Set
 End Property
End Class

这是控件的标记:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="UsrCtl_WebUserControl" %>
<span>Control Generated <%=DateTime.Now%></span>

它只输出当前时间。

下面是嵌入在一个页面中的用户控件:

<uc:wuc ID="wuc" runat="server" CacheString="A" />

在另一个页面中:

<uc:wuc ID="wuc" runat="server" CacheString="B" />

根据文档,此控件应为 CacheString 属性的每个值维护一个不同的 60 秒缓存版本。

它不起作用 - 它缓存 60 秒,但无论我在 CacheString 属性中放入什么内容,都只会创建一个缓存副本。

有人知道我做错了什么吗? - 4 小时后,我的头发或指甲都没有剩下了 - 请把我的显示器从砖块上救下来。

Here's my control's code behind:

<PartialCaching(60, Nothing, "UsrCtl_WebUserControl.CacheString", Nothing, True)> _
Partial Class UsrCtl_WebUserControl
 Inherits System.Web.UI.UserControl

 Private _CacheString As String

 Public Property CacheString() As String
  Get
   Return _CacheString
  End Get
  Set(ByVal value As String)
   _CacheString = value
  End Set
 End Property
End Class

Here's the Control's Markup:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="UsrCtl_WebUserControl" %>
<span>Control Generated <%=DateTime.Now%></span>

It just outputs the current time.

Here's the user control embedded in a page:

<uc:wuc ID="wuc" runat="server" CacheString="A" />

And in another page:

<uc:wuc ID="wuc" runat="server" CacheString="B" />

According to the docs this control should maintain a different, 60 second cached version for each value of the CacheString property.

It doesn't work - it caches for 60 seconds, but only one cached copy is created regardless of what I put in the CacheString property.

Anyone any ideas what i'm doing wrong? - After 4 hours of this I have no hair or nails left - please save my monitor from the brick.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2024-09-07 23:00:03

好吧,我花了一点时间,但我只是复制了你的问题。当两个控件在多个页面上具有相同的 ID 时,就会出现问题,并且在 PartialCaching 属性的构造函数中,将 Shared 设置为 代码>真。根据文档此处构造函数中的 Shared 属性为“true”指示用户控件输出可以与多个页面共享',这意味着,正如您所见,加载的第一个控件会设置它,后续控件只能读取已经存在的内容。在幕后,控件似乎仅根据控件的 ID 进行缓存,而不考虑控件所在的页面。

因此,有两种可能的解决方案:

  • 更改控件上的 ID
    page
  • 在 PartialCaching 构造函数中,
    将共享设置为 false。

OK it's taken me a little while but I just replicated your problem. The problem crops up when the two controls have the same ID across multiple pages and in the constructor for the PartialCaching attribute, you set Shared to True. According to the documentation here the Shared property in the constructor is 'true to indicate that the user control output can be shared with multiple pages', which means, as you've seen, the first control to get loaded sets it and subsequent controls can only read what's already there. Under the covers it seems the control gets cached based on the ID of the control only without any regard to the page the control is on.

So, there are two potential solutions:

  • Change the ID of the control on the
    page
  • In the PartialCaching constructor,
    set Shared to false.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文