Solved

ASP.NET Preventing Table insert after clicking on page refresh

Posted on 2009-04-14
10
548 Views
Last Modified: 2012-05-06
I have a page that inserts a record into a table upon clicking submit, how can I prevent a duplicate insert from occurring if the user clicks on Refresh after clicking on submit?
0
Comment
Question by:ITHelper80
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 7

Expert Comment

by:nkhelashvili
ID: 24139683
Don`t you want to add primary key to your table???
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 24139710
I am adding primary keys to the table upon submit....I dont want a duplicate record to be inserted, which is what is happening now if the user refreshes the page
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24139769
one way to do this is to have a status flag.So once submitted change the flag to false .Make it true only when you have new data.
So when updating,check for this flag and do the update.
0
 
LVL 7

Expert Comment

by:nkhelashvili
ID: 24139822
then you must refresh session variables (if you have)  on page refresh
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24139842
http://www.gklein.org/tests/js/disablekey.html

function disableKey(event) {
  if (!event) event = window.event;
  if (!event) return;

  var keyCode = event.keyCode ? event.keyCode : event.charCode;

  //window.status = keyCode;
  //alert(keyCode);
 
  // keyCode for F% on Opera is 57349 ?!
 
  if (keyCode == 116) {
   window.status = "F5 key detected! Attempting to disabling default response.";
   window.setTimeout("window.status='';", 2000);

   // Standard DOM (Mozilla):
   if (event.preventDefault) event.preventDefault();

   //IE (exclude Opera with !event.preventDefault):
   if (document.all && window.event && !event.preventDefault) {
     event.cancelBubble = true;
     event.returnValue = false;
     event.keyCode = 0;
   }

   return false;
  }
}

function setEventListener(eventListener) {
  if (document.addEventListener) document.addEventListener('keypress', eventListener, true);
  else if (document.attachEvent) document.attachEvent('onkeydown', eventListener);
  else document.onkeydown = eventListener;
 
  if (!document.getElementById) return;
  var el = document.getElementById("Msg");
  if (el) el.innerHTML = "Event handler added.";
}

function unsetEventListener(eventListener) {
  if (document.removeEventListener) document.removeEventListener('keypress', eventListener, true);
  else if (document.detachEvent) document.detachEvent('onkeydown', eventListener);
  else document.onkeydown = null;
 
  if (!document.getElementById) return;
  var el = document.getElementById("Msg");
  if (el) el.innerHTML = "Event handler removed.";
}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Expert Comment

by:spprivate
ID: 24140198
Right,typically thats what we do.As soon as the order is submitted we clean out the shopping cart.That way it avoids duplicate orders
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 24140481
Im not a javascript person so Gifts.... post is complete greek to me.....in any event I placed it into my aspx page and it didnt prevent the refresh from occuring.

Spprivate, can you give me an example of how you set the status flag?
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24140707
Declare a boolean IsUpdate = false
say wherever you build your productlist or cart have a global variable called
Set this flag to true
say in your btn_click event
before calling update method check
if(isupdate)
{
update
clear cart or productlist=""
isUpdate=false
}

Hope you got the idea
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24141111
Hi,

It is simple. Just copy the code to a javascript file and refer the file by <script type="text/javascript" src="<path>"> </script>

On the Submit button click event after you have updated the values into the database include the following code snippet.

Page.ClientScript.RegisterClientScriptBlock(btnSubmit.GetType(),"DisableKey", "setEventListener(disableKey)",true);
function disableKey(event) {

  if (!event) event = window.event;

  if (!event) return;
 

  var keyCode = event.keyCode ? event.keyCode : event.charCode;
 

  //window.status = keyCode;

  //alert(keyCode);

  

  // keyCode for F% on Opera is 57349 ?!

  

  if (keyCode == 116) {

   window.status = "F5 key detected! Attempting to disabling default response.";

   window.setTimeout("window.status='';", 2000);
 

   // Standard DOM (Mozilla):

   if (event.preventDefault) event.preventDefault();
 

   //IE (exclude Opera with !event.preventDefault):

   if (document.all && window.event && !event.preventDefault) {

     event.cancelBubble = true;

     event.returnValue = false;

     event.keyCode = 0;

   }
 

   return false;

  }

}
 

function setEventListener(eventListener) {

  if (document.addEventListener) document.addEventListener('keypress', eventListener, true);

  else if (document.attachEvent) document.attachEvent('onkeydown', eventListener);

  else document.onkeydown = eventListener;

}
 

function unsetEventListener(eventListener) {

  if (document.removeEventListener) document.removeEventListener('keypress', eventListener, true);

  else if (document.detachEvent) document.detachEvent('onkeydown', eventListener);

  else document.onkeydown = null;

}

Open in new window

0
 
LVL 6

Accepted Solution

by:
ITHelper80 earned 0 total points
ID: 24147703
Thanks for all the suggestions but I decided it would be easier to run a 2nd query to check for duplicates and then exit the Sub if the reader found a record.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now