0

I´ve an WebForms application with a simple form which persists values in the DB. The problem is: if I refresh this page (a simple F5), even when the form fields are empty, the last values are persisted again in the DB.

I just want to disable this behavior. How can I do this?

Thanks!

Rodrigo Abib
  • 83
  • 1
  • 2
  • 14
  • what do you mean when you say the following `he problem is: if I refresh this page (a simple F5), even when the form fields are empty, the last values are persisted again in the DB` can you show some of the Page_Load Code...?? – MethodMan May 22 '14 at 18:30
  • 1
    of course it will. it is a postback being re-executed again. you cannot prevent this from happening. One thing you can do is to set some page/viewstate flag and check to see if the operation was already executed. Alternatively do some checking in your DB before inserting to make sure the record is not inserted again – Ahmed ilyas May 22 '14 at 18:31
  • @Ahmedilyas I see. And if I put EnableSessionState as "false"? `<%@ Page Language="C#" AutoEventWireup="True" EnableSessionState="false" CodeBehind="..." Inherits="..." %>` – Rodrigo Abib May 22 '14 at 18:36
  • 1
    sessionstate has nothing to do with this scenario – Ahmed ilyas May 22 '14 at 18:44

1 Answers1

1

This can be solved with the post-redirect-get pattern. When submitting the form (POST request) the server redirects to a page (e.g. the detail page of the object just modified). This way you "clear" the POST request from the browser history. The user can easily navigate back without having anything saved again. Of course, if he hits the submit button again the form will be stored again.

CodeZombie
  • 5,367
  • 3
  • 30
  • 37