How to prevent duplicate posting on a form w/ refresh?

D

D. Shane Fowlkes

This may be a very simple question but I'm stumped none the less.

I have a form where a user provides comments. There's a Grid below this
form which displays all comments in the table so far. On the Page_Load sub,
this grid is loaded. When they submit the form, a SaveComments Sub will
insert the record into a table and then disable the button and textbox
(greyed out) and display a message in a asp:label saying "thank you...".
The last step of the SaveComments Sub is to reload the Grid which will show
the user's newly provided comments to the list of other comments.

Simple enough.

The button is disabled so they can't submit comments a second time during
that session. But I've discovered that if you refresh/reload the page,
it'll run the Sub all over again....inserting another record each time you
reload the page and the duplicates start appearing in the Grid. D'oh!!

So how can I easily prevent this? The only solution I can come up with is to
redirect them to another confirmation page. Not what I was hoping for.....

I'm using VB as the base code.
 
P

Patrice

Now what if they come the next day to add the exact same comment ? How can
you prevent this ?

You have basically two visions here :
- either accept it, this is the exact semantic for refresh : it repeats the
last HTTP request meaning submitting again the same data if you were
submitting (and most browsers should display a warning)
- you could tweak your app to disallow this. Hard to say wihtout knowing
your app but it looks like you meant you accept a single comment by session
? You could then if the session is the same update the current record or
just discard the input.

My personal preference is rather 1 (especially if they can delete the
comments) unless it would cause something irreversible and.or harmfull (such
as ordering a second car or something like that).

Patrice
 
G

Guest

I love those payment screens that warn you not to click submit twice!

You might try something like this...

private bool SubmitWasClicked {
get { return (bool)ViewState["SubmitWasClicked"]; }
set { ViewState["SubmitWasClicked"] = value; }
}

private void Page_Load(object sender, System.EventArgs e) {
if ( ! this.IsPostBack ) {
SubmitWasClicked = false;
}
}

private void Submit_Click(object sender, System.EventArgs e) {
if ( ! SubmitWasClicked ) {
Label2.Text = "Submit was clicked for the first time.";
// TODO - Your processing goes here
SubmitWasClicked = true;
} else {
Label2.Text = "Nice Try!";
}
}
 
G

Guest

Don't mind me, that trick never works.

Brad Quinn said:
I love those payment screens that warn you not to click submit twice!

You might try something like this...

private bool SubmitWasClicked {
get { return (bool)ViewState["SubmitWasClicked"]; }
set { ViewState["SubmitWasClicked"] = value; }
}

private void Page_Load(object sender, System.EventArgs e) {
if ( ! this.IsPostBack ) {
SubmitWasClicked = false;
}
}

private void Submit_Click(object sender, System.EventArgs e) {
if ( ! SubmitWasClicked ) {
Label2.Text = "Submit was clicked for the first time.";
// TODO - Your processing goes here
SubmitWasClicked = true;
} else {
Label2.Text = "Nice Try!";
}
}

D. Shane Fowlkes said:
This may be a very simple question but I'm stumped none the less.

I have a form where a user provides comments. There's a Grid below this
form which displays all comments in the table so far. On the Page_Load sub,
this grid is loaded. When they submit the form, a SaveComments Sub will
insert the record into a table and then disable the button and textbox
(greyed out) and display a message in a asp:label saying "thank you...".
The last step of the SaveComments Sub is to reload the Grid which will show
the user's newly provided comments to the list of other comments.

Simple enough.

The button is disabled so they can't submit comments a second time during
that session. But I've discovered that if you refresh/reload the page,
it'll run the Sub all over again....inserting another record each time you
reload the page and the duplicates start appearing in the Grid. D'oh!!

So how can I easily prevent this? The only solution I can come up with is to
redirect them to another confirmation page. Not what I was hoping for.....

I'm using VB as the base code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top