Output Caching in UserControl

G

Guest

Hi,

I have a strange problem with a usercontrol on a page. The usercontrol
dispalyes three categories (From a database) when the user clicks a category
they see all the products in a shop for that category, the results are paged
10 to a page and the user can page them.

As this "Category" usercontrol hardly ever changes I wanted to setup up
caching for it. However I realised that when the user clicks a ctageory it
is selected and therfore I would need to cache each instant of the control
varied by the selected category ID. As the category ID is in the querystring
I used varybyparam. Here is my output cache directive for the usercontrol:

<%@ OutputCache Duration="360" varybyparam="CategoryID" shared="true" %>

There is a problem though. When the user selects a category, the correct
one is dispalyed and is shown as selected. Howevere when they click "next
Page" the wrong cahced version of the usercontrol is displayed.

I have stepped through this. I placed a breakpoint in the page load of the
usercontrol. This is what happens:

(When I click on a category a redirect is performed, to the same page but
changing the CategoryID querystring)

1) First page loads - Usercontrol is loaded from DB. (Breakpoint Hit)
2) I click on Category1 - Usercontrol is loaded from DB. (Breakpoint Hit)
3) I click on Category2 - Usercontrol is loaded from DB. (Breakpoint Hit)
4) I click on Category3 - Usercontrol is loaded from DB. (Breakpoint Hit)

2) I click on Category1 - Usercontrol is loaded from CACHE.
3) I click on Category2 - Usercontrol is loaded from CACHE.
4) I click on Category3 - Usercontrol is loaded from CACHE.

5) I click "Next Page". Postback Occurs. Usercontrol is loaded from CACHE.
Wrong cached instance is used though.

It seems as though after a postback the varybyparam is going awol.

Can anybody Help?

PS The usercontol has viewstate disabled, but enabling it makes no diffrence.
 
A

Andy Mortimer [MS]

Hi,
I've seen similar problems myself, but never had the opportunity to
research too far into it. If you can supply me with an accurate repro I may
be able to make some progress.

Andy Mortimer [MS]
Please do not send email directly to this alias. This alias is for
newsgroup purposes only

This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
 
G

Guest

Do you want code so that you can reproduce this on your client?

If this is what you want I can give this. you can see the action here
http://www.trendytadpoles.co.uk/default.aspx

The user control is the top Category Menu (GIRLS | BOYS | UNISEX). I have
set the duration to 1 for this site. so it seems like it is working when you
look.

When I increase the duration to say 60, when you click on Girls and then
click ion the "Next Page" paging control, the Category "GIRLS" looses its
selection. The page has loaded the cached version of the control where no
category is selected. This cahced version is the version that is created
when the site first loads.


B..M

The URL I gave has the site, but as I said I had to set the duration to 1 so
that it works correctly. The site has not launched yet but I don't want it
to be buggy for my cleitn whilsty she is setting the site up (adding products
etc) When the duration is set to 1 the control is never cached and therefore
works, but I am not getting any performance gains through cacheing.

If you will both find it helpful I can create a test area to show you it in
action when it fails?



I appreciate both your input...
 
A

Andy Mortimer [MS]

I'm not sure who your are replying to (both?). I will need to see a small
cut down reproduction of the problem which I can build and use to reproduce
the problem here. Can you cut out everything from that code (it may be
simpler to create a new app) and then put it on an ftp location which I can
download from? Thanks.
Andy
 
G

Guest

OK I have created a reproduction page to show the error in its full
technicolor!

http://www.trendytadpoles.co.uk/TestArea/CacheTest.aspx

The three links are in one usercontrol. When a link is clicked it will be
disabled, so you can see which link is clicked.

When you do a postback using the button the usercontrol that was cached
first is displayed.

It seems to mess up with Postbacks, when you click each link over and over
you can see it is using the cached instance, only after a postback does it
fail. It loses the varybyparam on postback.

No code is ran when you click the button it is just for a postback.

Any ideas now?
"When you do a postback using the button the usercontrol that was cached
first is displayed" - This is the error!!!!
 
E

Elron

This is strange indeed. Basically there is no difference between
(a) clicking the postback button and (b) submitting the link url,
right?
But you see "
- (a) is a POST and (b) is a GET.

I wonder what will happen if you can try an ordinary button instead of
a submit button?
 
E

Elron

This is strange indeed. Basically there is no difference between
(a) clicking the postback button and (b) submitting the link url,
right?
But you see "
- (a) is a POST and (b) is a GET.

I wonder what will happen if you can try an ordinary button instead of
a submit button?
 
G

Guest

Yes it is strange.....

I will try the normal button tomorrow, just going to bed now :)

But it will just perform a postback the same won't it? On my LIVE site the
postback occurs through a linkbutton, not a submit button and the same error
occurs, in fact this is where my problem stems from in the first place.

I think you are on the right track with POST and GET. I never thought of
that....... Therefore do you think I need a hidden form element called
CategoryID that I populate in order for the Postback POST to cache
appropriately by param? I'm just thinking that the GET looks at the
querystring for its param and the POST may exclusively look at the FORM,
therefore adding a FORM element may fix the issue..hmmmm I'll try
that.....It means I will have 6 cached instances 3 for the GET and 3 for the
FORM......But that is better than no cache at all....

I'll post my results on here tomorrow...

Thanks for checking back, just knowing other people think it is strange
helps! Its not just me going mad! :)

I do find it strange how this has not come up on any forums, and I have
looked high and low....surely I can't be the first to try this?!?!?!?
 
A

Andy Mortimer [MS]

Can I get the code so I can build and reproduce here? Thanks

Andy Mortimer [MS]
Please do not send email directly to this alias. This alias is for
newsgroup purposes only

This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top