Page Refresh Causes Duplicate POST in ASP.NET Applications

假装没事ソ 提交于 2019-12-17 11:08:45

问题


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?


回答1:


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.




回答2:


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;
}



回答3:


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.




回答4:


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;
}



回答5:


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.




回答6:


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.




回答7:


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 :)




回答8:


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.




回答9:


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.




回答10:


After insert or update statement is executed you can redirect the user to the same page with

Response.Redirect(Request.Url.AbsoluteUri);



来源:https://stackoverflow.com/questions/743580/page-refresh-causes-duplicate-post-in-asp-net-applications

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