| Browser refresh problem.... |
| Sneha Choudhary posted at Monday, March 23, 2009 4:20 AM |
Hi,
I know I am going to ask a silly question. But I am
fighting with this problem for the last one week and unable to get the
result. So please assist me.
I am building a shopping cart
project. At a certain point there is a condition that whenever user
enters the same product again and again, only the quantity should be
updated. I have done that and is working fine. But the problem is : Suppose I am having a single item in the cart with
quantity "1". Whenever I click the browser refresh it automatically
increases the quantity of the item.
That is whenever I refresh the page it will update the quantity.
I searched a lot and got the conclusion that:
When page posts, browser keeps the posted data in cache and sends it
again when refresh button is pressed. So all the events executed before
will get executed again. To avoid this, do a redirect once you added an
item to shopping cart. Redirection will clear the post data.
// do shopping cart updation Response.Redirect("confirmation.aspx") In confirmation page, you can inform user that the quantity has been updated. Redirecting to same page will work as well.
But I am unable to understand where to apply the redirect. 1 protected void Page_Load(object sender, EventArgs e) 2 { 3 try 4 { 5 HyperLink HLink = (HyperLink)Master.FindControl("HyperLink3"); 6 if (HLink != null) 7 { 8 HLink.Enabled = false; 9 } 10 11 12 if (!Page.IsPostBack) 13 { 14 if (Session["Cart"] == null) 15 { 16 Lblmsg.Visible = true; 17 dt = new DataTable(); 18 dt.Columns.Add("REF", typeof(string)); 19 dt.Columns.Add("Description", typeof(string)); 20 dt.Columns.Add("QTY", typeof(int)); 21 dt.Columns.Add("Price", typeof(float)); 22 dt.Columns.Add("Cost", typeof(float)); 23 get_data(); 24 Session["Cart"] = dt; 25 26 } 27 else 28 { 29 Lblmsg.Visible = false; 30 dt = (DataTable)Session["Cart"]; 31 get_data(); 32 Session["Cart"] = dt; 33 } 34 } 35 } 36 catch(Exception ee) 37 { 38 Lblmsg.Text = ee.Message; 39 } 40 } 41 42 public void get_data() 43 { 44 try 45 { 46 pro_id = Request.QueryString["pr_id"]; 47 quanti = Convert.ToInt32(Request.QueryString["quant"]); 48 49 if (pro_id != null) 50 { 51 SqlConnection con = new SqlConnection("Server=.; Database=eclsc; Trusted_Connection=yes"); 52 SqlCommand cmd = new SqlCommand(); 53 cmd.Connection = con; 54 cmd.CommandText = "select ICEMACHINES.product_id,ICEMACHINES.sh_desc,ICEMACHINES.price from ICEMACHINES where ICEMACHINES.product_id LIKE @product_id union select GLASSWARE.product_id,GLASSWARE.sh_desc,GLASSWARE.price from GLASSWARE where GLASSWARE.product_id LIKE @product_id "; 55 cmd.Parameters.Add("@product_id", SqlDbType.NVarChar, 50).Value = pro_id; 56 cmd.Connection.Open(); 57 SqlDataReader rdr = cmd.ExecuteReader(); 58 ArrayList arRole1 = new ArrayList(); 59 60 while (rdr.Read()) 61 { 62 desc = (rdr["sh_desc"]).ToString(); 63 id = (rdr["product_id"]).ToString(); 64 unitprice = Convert.ToSingle((rdr["price"])); 65 cost = unitprice * quanti; 66 67 } 68 cmd.Connection.Close(); 69 70 int dtcount = dt.Rows.Count; 71 if (dtcount > 0) 72 { 73 string dc = dt.Columns[0].ColumnName; 74 string filter = String.Format("REF LIKE '{0}*'", id); 75 DataRow[] arrMatchingValues = dt.Select(filter, "REF"); 76 if (arrMatchingValues.Length > 0) 77 { 78 DataRow drUpdateMe = arrMatchingValues[0]; 79 int drUpdateQuant = Convert.ToInt32(drUpdateMe["QTY"]); 80 int quan = quanti + drUpdateQuant; 81 float cos = unitprice * quan; 82 drUpdateMe["QTY"] = quan; 83 drUpdateMe["Cost"] = cos; 84 dt.AcceptChanges(); 85 Session["data"] = dt; 86 GridView1.DataSource = dt; 87 GridView1.DataBind(); 88 89 } 90 else 91 { 92 DataRow myrow = dt.NewRow(); 93 myrow["REF"] = id; 94 myrow["Description"] = desc; 95 myrow["QTY"] = quanti; 96 myrow["Price"] = unitprice; 97 myrow["Cost"] = cost; 98 dt.Rows.Add(myrow); 99 dt.AcceptChanges(); 100 Session["data"] = dt; 101 GridView1.DataSource = dt; 102 GridView1.DataBind(); 103 } 104 } 105 else 106 { 107 DataRow myrow = dt.NewRow(); 108 myrow["REF"] = id; 109 myrow["Description"] = desc; 110 myrow["QTY"] = quanti; 111 myrow["Price"] = unitprice; 112 myrow["Cost"] = cost; 113 dt.Rows.Add(myrow); 114 dt.AcceptChanges(); 115 Session["data"] = dt; 116 GridView1.DataSource = dt; 117 GridView1.DataBind(); 118 } 119 120 } 121 else 122 { 123 int a = 1; 124 } 125 total_items = total_items + quanti; 126 total_Price = total_Price + cost; 127 Session["items"] = total_items; 128 Session["value"] = total_Price; 129 } 130 catch(Exception ee) 131 { 132 Lblmsg.Text = ee.Message; 133 } 134 }
Please check it out and suggest me where to use redirect. I followed these links:
http://blog.andreloker.de/post/2008/06/Post-Redirect-Get.aspx
http://forums.asp.net/t/1120319.aspx
waiting for your
response.
cheers
sneha |
 |
|
|
| |
| R::Browser refresh problem.... |
| egg egg replied to Sneha Choudhary at Monday, March 23, 2009 5:09 AM |
Seeing through the articles mentioned, the solution to redirect is to have three step process...
Whenever you want to save any data, the updated quantity in your case, you need to save the data to the database and redirect to a different page to get the data...
Still you need to proceed to do any updations, then have a link in the get page to the update page and do the stuff there...
|
 |
| |
| Re: Browser refresh problem.... |
| Vasanthakumar D replied to Sneha Choudhary at Monday, March 23, 2009 11:36 AM |
 | Hi,
you need to redirect after you store the values in sessions.... it may be in your save button..
pls post your full code.... |
 |
| |
|
|