Asp.Net SQL Refresh page duplicate inserts?

淺唱寂寞╮ 提交于 2019-12-02 05:25:55

If the PostButton_Click is the server handler for the click event on your button then insert code shold be executed only if the user hits the post button - it should not happen in case of refresh.

I am pretty sure is not enough - to be sure put a breakpoint in your PostButton_Click method, hit F5 to run in debug mode and see if the breakpoint gets hit with a refresh.

If so it means that:

  1. you are calling explicitly PostButton_Click from somewhere else (Page_Load?)
  2. PostButton_Click was accidentally set as event handler for some other event on the page

A quick way of verifying hypotesis 1 & 2 is to run a search for PostButton_Click and see where you are calling it from or if it set as handler for some other element (maybe your form?) in the markup.

Add Response.Redirect(Request.Url.ToString(), false); to your button event and that should solve the problem.

if he refreshes or press the back button the post will happen again, thus your page will process the same data thus insert once again.

You have to change your logic

It would be preferable to redirect the client to another page after the insert, however, if you really need the client stay in the same page, you can use a (bool) variable on your page to state if the insert has been done, and skip the insert logic if it has been executed before

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!