473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

AddToCart, Refresh issue (form variables)

I get the feeling this is a pretty classic problem, but I'm a bit of an uber
newber. Apologies!

Products page, user enters a quantity and clicks one of my "Add to Cart"
buttons, which bubbles up through the datalist and calls a method to
AddToCart(cartID,prodID,prodQty). Now, if the user clicks 'refresh', that
method is executed again without clicking the button. So if they add 2
apples, refresh, they have 4 apples, refresh, 6 apples, etc. I enable
tracing and I can see I've got a form variable that's holding onto the
quantity value- how do I clear it after executing the method?

I've seen this solved by passing the values to another aspx page that then
redirects back to the products page - a little bit classic-ASP style? I'd
prefer not to do that, in the spirit of .Net, and stick to PostBacks, but
I'm not quite sure how to do this. The code I've tried to use to clear the
form values must not be quite right.

Here's the code I'm working with, minus failed fix-attempts, if it makes it
easier to correct me (feel free to correct anything else I'm doing wrong -
my coding needs help). Thanks in advance!!! -John

-----------

private void DataList1_ItemCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
int prodID =
Int32.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());

TextBox qtyField = (TextBox)e.Item.FindControl( "txtQty" );
int prodQty = Int32.Parse(qtyField.Text);

ShoppingCart cart = new ShoppingCart();
string cartID = cart.GetCartId();

try
{
if(quantity > 0)
{
qtyField.Text = "0";
cart.AddToCart(cartID, prodID,prodQty); //quantity non-int!? fix
}
}
catch
{
//TODO: update for problem adding item to database
}
}
Nov 18 '05 #1
4 2016
I've been looking at a bunch of different solutions, and none of them were
quite what I needed. But I think I've got one, now, that's a lesser evil:

Response.Redirect(Request.RawUrl);

I put this in a finally block, after my original try/catch. So after
processing the first submit event, the page redirects to itself, clearing
variables. I guess this essentially does the same thing that the
post-to-another-page solution does, but is one hop less. This is the only
way I could get those stupid forms variables to clear properly. Couldn't get
Server.Transfer to cooperate, either. But then, I'm clueless.

Lemme know if anyone has anything better. Thanks!

-John
Nov 18 '05 #2
Self-correction.. guess it wouldn't be one hop less. Eh.
processing the first submit event, the page redirects to itself, clearing
variables. I guess this essentially does the same thing that the
post-to-another-page solution does, but is one hop less. This is the only

Nov 18 '05 #3
When you hit refresh, the browser re-plays the post that was used to
create the page, so it doesn't matter if you clear the form values in
the post-back, because refresh isn't using the values that are
displayed, it's using the values that the browser has cached from the
previous post.

One solution is using a Redirect, as you've done, to break the returned
page free of the post, so refresh will re-play the get of the redirect
instead.

Another technique is to embed a unique key value in the form as a hidden
field. Each time you generate the page you embed a new unique key value,
and each time you process a post you check to see if the key has
already been used (by keeping a list of used keys on the session or
something). If someone re-plays a post with a used key you can just
ignore it, or perhaps issue an error message telling them not to refresh!

-Jason

John wrote:
I've been looking at a bunch of different solutions, and none of them were
quite what I needed. But I think I've got one, now, that's a lesser evil:

Response.Redirect(Request.RawUrl);

I put this in a finally block, after my original try/catch. So after
processing the first submit event, the page redirects to itself, clearing
variables. I guess this essentially does the same thing that the
post-to-another-page solution does, but is one hop less. This is the only
way I could get those stupid forms variables to clear properly. Couldn't get
Server.Transfer to cooperate, either. But then, I'm clueless.

Lemme know if anyone has anything better. Thanks!

-John

Nov 18 '05 #4
Thanks for the clarifications! I'll have to consider the unique key
approach. Too bad IsPostBack can't identify refreshes (maybe by putting a
unique key in the ViewState?). At least it seems to be working, now. Thanks
again!

-John

"Jason DeFontes" <ja***@defontes.com> wrote in message
news:eG**************@TK2MSFTNGP12.phx.gbl...
When you hit refresh, the browser re-plays the post that was used to
create the page, so it doesn't matter if you clear the form values in
the post-back, because refresh isn't using the values that are
displayed, it's using the values that the browser has cached from the
previous post.

One solution is using a Redirect, as you've done, to break the returned
page free of the post, so refresh will re-play the get of the redirect
instead.

Another technique is to embed a unique key value in the form as a hidden
field. Each time you generate the page you embed a new unique key value,
and each time you process a post you check to see if the key has
already been used (by keeping a list of used keys on the session or
something). If someone re-plays a post with a used key you can just
ignore it, or perhaps issue an error message telling them not to refresh!

-Jason

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Baker | last post by:
HI again: I have made 2 posts on this issue, and for some reason still haven't got the right result. I have a form which contains a number of totals. These totals use unbound control boxes and...
3
by: Danny | last post by:
HI How can I refresh the main project window in code in ms access 2000 For example, I create a new table, but when I minimize the form, the table is not there until I do F5 (refresh). I do...
5
by: Andrew Chanter | last post by:
I have a situation where I am using an unbound dialog form to update data in an Access 2002 split back end / front end scenario. The data update is done via an ADO call (direct to the back end...
9
by: PK9 | last post by:
I'm having an issue with the "Refresh" of an asp.net page. The refresh is actually calling my last onClick event. I thought that asp.net was supposed to be stateless in that it shouldn't...
17
by: Jim Little | last post by:
Hello, I'm driving myself crazy either because I'm missing something about ASP.NET, or what I'm trying to do simply can't be done. First, I am not using session variables to track state. My...
2
by: Robert | last post by:
I am trying to give the user dynamic search capabilities to select almost any record in the database from criteria they select. Everything seems to work except when I open the display form to...
3
by: Ned Balzer | last post by:
Hi all, I posted this question some time ago in an earlier thread but so far I still don't have an understanding of why this is happening or what I can do to fix it. I use Session variables,...
10
by: markwalker84 | last post by:
Hello everyone! Got a bit of a problem... Two of the panels on my program contain a number of check boxes. The exact number of which is determined by a pair of variables. I have recently...
6
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi - I'm having trouble refreshing a datagrid control bound to a dataset that was created from the New Data Source wizard. What is the code required to refresh the datagrid with data from the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.