views:

41

answers:

1

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.

A: 

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.
PhilPursglove
Hi Phil,Thanks, but if you read my question, you'll see that i'm using the controls on different pages, not on the same page.I need Shared="True" as this is to cache a page footer - there'd be little point caching the page footer on a page by page basis!I can't see what i'm doing wrong. Thanks for the attempt though!
PapillonUK
@PapillonUK The same control on two separate pages is how I replicated it, but now I'm slightly confused - you want them cached seperately on each page but they're both in a common footer? Can you put some of your page markup into your question? My demo code is at http://cid-5e6d39865220496a.skydrive.live.com/self.aspx/StackOverflow/PartialCaching.zip
PhilPursglove
All the code is above, I've now added the control's markup - it just displays the current time. This is not the full code, i took out everything but the bare min.I intend to use a control to cache a footer area on many pages on my website. The footer area will be the same on ALL pages apart from one small aspect which I expose as a property of the control - hence the need to vary the cache by a property.Tried using a different ID on different pages - same issue. Cached for 60 secs but only one cached version regardless of the property value specified.
PapillonUK
Correction (and apology!): When I changed the control ID's it WAS working (I was getting odd results due to testing before the old cache had expired). So, since I need "Shared=True" as that's the whole point, it looks like I'd have to have a unique ID on every page! That's crap, how could I manage that. I really don't want to have to use LoadControl and dynamically add an ID. This looks like a bug - .NET is obviously just checking the ID and none of the other cache dependancies. Grr!.
PapillonUK