3

I have a user control in my asp.net application that load it's data from cache. i want it's cache be updated when the value of a variable be changed so i put this code on page directive

 <%@ OutputCache Duration="1000" VaryByParam="none"
 VaryByControl="visitIsAu" %>

it works fine when i change the value of variable in markup like this

 visitIsAu="true"

but it doesn't work when the value is changed through code behind like this

   visitIsAu="<%=this.CurentUser.IsAuthorizedToVisitFiltered%>"

the value of variable does not change so its cache is not updated.

does anyone know why this happen?

RickNZ
  • 18,448
  • 3
  • 51
  • 66
Vahid Ghadiri
  • 3,966
  • 7
  • 36
  • 45
  • Maybe replacing it with `visitIsAu="<%#this.CurentUser.IsAuthorizedToVisitFiltered%>"`? – Uwe Keim Nov 06 '11 at 11:37
  • 1
    @Uwe Keim: i tried that but it didn't work. – Vahid Ghadiri Nov 06 '11 at 11:42
  • sounds like page life cycle problem. cant you just set the value on a preceding event like pageInit in server side ? I think it does not work as the visitIsAu parameter evaluation is fired after the cache has been set, so you will have to change it before that... – AMember Nov 06 '11 at 14:08
  • What type of control is visitIsAu ? And I think you want to set the value in markup and not in code behind. Let us know if I am right. If so you can solve it by – Vikram Shetty Nov 15 '11 at 04:44

1 Answers1

1

For some reason I've never trusted the way ASP.NET handles cache using markup settings or using "injected" server tags on the aspx files. I know it works but it's only easy to control on simple scenarios.

I always implement cache control, when using user controls, at code behind level and taking into consideration that page life cycle that may have some catches base on the level of controls I have a certain page.

We can have: ascx > aspx or ascx > aspx > master.

In your case you need to guarantee that CurrentUser is set prior to the load of your user control. Check that using debug.

Rui Marques
  • 3,429
  • 1
  • 22
  • 26