Reputation: 131
i have an application in which i am adding a new record and the record is added two times if i am pressing refresh button.
I am clearing the cache but i am having the same problem. what to do?
Upvotes: 13
Views: 15611
Reputation: 659
After insert or update statement is executed you can redirect the user to the same page with
Response.Redirect(Request.Url.AbsoluteUri);
Upvotes: 0
Reputation:
If you don't have any session (and have cookies enabled) you could use:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (IsPostBack && ViewState[REFRESH_CHECK_GUID] != Request.Cookies[REFRESH_CHECK_GUID])
{
IsRefresh = true;
}
String checkGuid=System.Guid.NewGuid().ToString();
Response.Cookies[REFRESH_CHECK_GUID] = checkGuid;
ViewState[REFRESH_CHECK_GUID] = checkGuid;
}
/// <summary>
/// True if the last PostBack was caused by a Page refresh.
/// </summary>
public virtual bool IsRefresh
{
get;
private set;
}
Upvotes: 2
Reputation: 5427
My method is shamelessly stolen from page 4 of this awesome article on ASP Alliance. To detect a refresh, you can store the same value in ViewState and Session. If the user refreshes a page, it will PostBack using the old ViewState data that does not match your Session value. Once you detect this, you can create a Page that all of your other Pages inherit from with a simple variable to test for whether the user refreshed the browser. This solution requires ViewState to be enabled and a valid Session.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (IsPostBack && ViewState[REFRESH_CHECK_GUID] != Session[REFRESH_CHECK_GUID])
{
IsRefresh = true;
}
Session[REFRESH_CHECK_GUID] = System.Guid.NewGuid().ToString();
ViewState[REFRESH_CHECK_GUID] = Session[REFRESH_CHECK_GUID];
}
/// <summary>
/// True if the last PostBack was caused by a Page refresh.
/// </summary>
public virtual bool IsRefresh
{
get;
private set;
}
Upvotes: 8
Reputation:
If you need to stay in the same page, then I recommend you redirect the user to a confirmation page where you can then ask the user if they want to insert another record, if so then you redirect them back to the page where they can insert records, this will help prevent the duplicate inserts. Or they can just go to a "main" page if they are done inserting records. This is what I have done in the past when I needed users to be able to "stay" in the same page after an insert.
Upvotes: 0
Reputation: 14229
As David M said above, the best idea is to redirect after the post. The affect of having another server round-trip is pretty minimal, as only the http headers are sent in a redirect.
Upvotes: 0
Reputation: 2055
Even if we use Ajax for whole form, it gives the same duplicate insert, but we can try by clearing hiddenfield _EventTaret and change the focus of some other control :)
Upvotes: 0
Reputation: 25775
In the ideal scenario, a HTTP POST submit that causes a database update would redirect after successful update to a secondary page which informs the user about the success of the operation. If the user tries to go back to the previous page, the browser would prompt the user with a message similar to ""The page cannot be refreshed without resending the information. Click Retry to re-send the information or click cancel to continue". This should suffice as user intimation that refreshing the page will entail duplicate submission. If the update failed, however, the same page would load and allow the user to retry.
Of course, this is not a hard and fast rule and implementations vary. I would greatly recommend taking @edg's suggestion into practice - Your database insertion code should always check duplicacy before applying an insert/update.
Upvotes: 3
Reputation: 187030
The problem is due to the hidden value that resides in __EVENTTARGET that cannot be set through code.
If you can use Ajax to add your data then this issue can be solved.
Upvotes: 0
Reputation: 72850
Once you have handled a post back to a page, best practice is to then redirect so that you can't have the data posted a second time using the "refresh" button. This is common to all web develoment, not just ASP.NET.
Upvotes: 12
Reputation: 35247
If the application is yours, you need to add code that checks first that the record does not exist before attempting to save it to your database.
Upvotes: 1