Session State and debugging the code behind

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

I'm writing a shopping cart in vb.net and for the first time I'm working
with session state on the server. I'm using it to maintain the user's
shopping cart for the session. in the browser is a datagrid with a listing
of products. If I put a break point in the code behind the code execution
will break at that point just as you would expect. When the user clicks on
an item in the grid it executes a line of jscript listed below which posts
the item to the shopping cart:
parent.data.frameElement.src="data.aspx?Task=CartAddNew&sku=" + sku;

However, this only works correctly the first time a grid item is clicked on.
if the user clicks on any of the previous clicked on items the line above
will run and I can see the shopping cart summary update in the page (which
means that the code behind is executing), but none of the code behind will
break on the break points. This only started happening after I started
using session state on the back end. Is it possible that session state is
causing this and do you have any ideas on how to begin trouble shooting
this? Its like a ghost I can't identify.

Thanks.
 
Hello,

Thank you for using the community. From the description, it is hard to tell
if this problem is related to the session. More source code may provide
useful detail information about the problem.

Generally, when the script is executed, the page will be refresh and code
in Form_Load will be executed. You may set a break point at the beginning
of this sub to see if it will be executed, or you can add some debug code
like:

Response.Write "Here has been executed"

This will indicate if a sub has been executed.

Additionally, since it work in the first time, but failed later, you may
check the client source code to see if there is any difference between them.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
I already tried your suggestion by putting alerts in the jscript which
flagged where the code was executing. I also but numerous break points
through out the code behind. All code was in fact executing because the
code behind was recalculating the shopping cart total with each postback,
but the code behind stopped breaking on the break points once I clicked on a
shopping cart item for the 2nd time. Since this is running in a frames
page, could that have anything to do with this? I wouldn't thing so because
all shopping cart functionality happens via a hidden frame and an aspx page
called data.aspx (meaning that i'm not using various frames and or pages to
make the round trips).
 
If you right click in the frame and select "Refresh", will the break
points get fired? Also, as I suggest, add some code in the code behind like:

Response.Write "Here has been executed"

Have you seen the line "Here has been executed" after click the item for
second time?

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
OK I figured out the problem, but not the solution. First, let me clarify
how my pages are setup: I'm using frames and there are 3 frames involved in
this process, their aspx pages are:
data.aspx, this page (and frame) is hidden and data.aspx is used to martial
parameters back and forth.
ProductListing.aspx has a datagrid with inventory items and their images (a
user clicks on an image and the item is added to the shopping cart via
data.aspx).
leftMenu.aspx is the page on the left hand side which has a menu for product
categories and also has the shopping cart total.

When the user clicks on the first image in ProductListing, a jscript
function passes the sku number to data.aspx for a postback like this:
parent.data.frameElement.src="data.aspx?Task=CartAddNew&sku=" + sku;

The 'Page_Init. method in data.aspx passes the sku to another method that
added the item to the shopping cart, and then writes shopping cart new total
to an invisible textbox in data.aspx. So when data.aspx reloads, another
jscript function takes that total and writes it to a textbox in
leftMenu.aspx.

Evidently, when the sequence of clicking on images to add them to the
shopping cart goes like this:
Click on the first image - its added to the cart (cart total = $10)
Click on the second image - its also added to the cart (cart total = $20)
Click back on the first image again - (cart total goes back to $10) and no
server side code executes because the first instance of data.aspx is pulled
from cache. This is why for example if all items cost $10, the total goes
to $20 when the second item is clicked on, and then back to $10 (instead of
$30) when the first image is clicked on again.

To verify this I put 'Response.Write("Here has been executed " & Now)' in
the data.axpx Page_Init sub. When I click back on any items in the datagrid
after already clickng on them for a first time, the timestamp goes back to
thier original timestamps.

Solution: How do I prevent instances of data.aspx from being cached on the
client (or anywhere for that matter)?
I don't have any tags like the one below in data.aspx:
<%@ OutputCache Duration="1" VaryByParam="someparam" Location="Client" %>
 
I forgot to mention: I can get it to behave correctly only when I set the
browser settings to:
tools/General-Settings/ and check "Every Visit To Page".

The problem is that hopefule most browsers will have a defualt setting other
than this one so caching of pages on the client side can work when you want
it to. Is there a way to override this setting for this particulare page
(data.aspx) so it will always get a new version and not a cached version?
 
OK I got it.

In the Page_Load event add this line of code:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

and the page doesnt get cached on the client no matter what.
 
Back
Top