By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,423 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

AddToCart, Refresh issue (form variables)

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.