Priyank Patel
Priyank Patel

Reputation: 6996

How to solve the issue of postback through browsers reload button in Asp.Net

I am working on a page in ASP.NET/C# where the user's information is saved to the database.
There are many fields on the page and when the validations are done then the information is saved.

The submission of information is done using an ASP.NET button.
The primary key of the table is generated in code itself, that is, every time it is increased by one.

I just faced an issue while reloading the page through the browser's reload button. I noticed that the same user information was submitted with the primary key value incremented by one.

So my question is how should I tackle this issue?
Should I just clear values of all textboxes after successful submission of data so that it will validate and then the user has to fill the information again?

Please share your ideas as to how can I handle this.

Upvotes: 0

Views: 709

Answers (1)

Richard
Richard

Reputation: 30618

The easiest thing to do use use the Post/Redirect/Get pattern, where after a postback where you have inserted the row, you redirect the user back to a page where they can view the item. This way, if they press Refresh, it will just refresh the view page.

For example:

protected void Button1_Click(object sender, EventArgs e) 
{
   // Insert Data
   ...

   // Redirect to get
   Response.Redirect("ViewItem.aspx?id=" + insertedItemId);
}

NB: They can still press back and submit again, but this is usually confirmed by the browser before resubmitting.

Upvotes: 7

Related Questions