How to prevent repeated postbacks from confusing m

2019-01-19 20:05发布

I have a web application (ASP.Net 3.5) with a conventional 3 layer design. If the user clicks a button a postback happens, some middle and data layer code runs, and the screen is refreshed. If the user clicks the button multiple times before the first postback is completed my logic gets confused and the app can end up in an invalid state. What are the best ways to prevent this?

I can use javascript to disable the button but this just hides the problem. How do I build my business and data layers to handle this?

7条回答
我命由我不由天
2楼-- · 2019-01-19 20:46

The three most popular methods (which are often used in tandem) are:

  1. Disable submit buttons once clicked/pressed;
  2. Use POST+REDIRECT+GET to avoid back button issues; and
  3. Replace history in the browser so you can't go back (but this should be used sparingly and with good reason).
查看更多
狗以群分
3楼-- · 2019-01-19 20:48

If I was to be brutally honest, I would say that it sounds like you're the one confused about web postbacks, not your application (that's if you're the one who wrote it). ;-)

That said, in addition to other suggestions, what I would do in this case is place a "token" in hidden field in the form - like a GUID - that is posted back. Use this to track the work being done and only allow it to be used once. E.g. when posted back, place it in session storage. Each time a postback is performed check the session first for this token, and if it is there then do nothing. If it's NOT there, store it in session and do the work. When the session ends, tokens are thrown away automagically. Easy. Much better than some convoluted database token.

  • Oisin
查看更多
女痞
4楼-- · 2019-01-19 20:57

Check out this ASP.NET AJAX control called PostBack Ritalin from a fellow SO'r Dave Ward.

查看更多
唯我独甜
5楼-- · 2019-01-19 20:58

I have solved the problem writing a javascript disabling the click function button:

MyButton.Attributes.Add("onclick",
    "javascript:this.onclick=function(){return false;};");
查看更多
劫难
6楼-- · 2019-01-19 20:59

Even i got the Same Problem I have resolved like Below.

After uploading a File If you Redirect to same page or some other page in your project this problem will be avoided.

For Example:

In My ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebApplication.WebForm" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        </div>
    </form>
</body>
</html>

Even i got the Same Problem I have resolved like Below.

After uploading the File If you Redirect to same page or some other page in your project. After Redirection Response will not be there once you redirected.

In My ASPX

In My Code Behind

public partial class WebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string path = Server.MapPath("~");
            path = path + FileUpload1.FileName;
            FileUpload1.SaveAs(path);
            Response.Redirect("WebForm.aspx"); // Responce will be cleared. This Redirection will do the Trick 
            //Put the debugger and check it will work
        }
    }

Here, to show the success and error messages try to use sessions.

查看更多
Melony?
7楼-- · 2019-01-19 21:04
  1. Do disable submit button once clicked. This will prevent accidental double-click or more
  2. I usually redirect to a different URL after postback to avoid accidental/intentional page refresh.
  3. Finally in your DB insert method, check for identical data inserted within certain time frame (probably in seconds) before doing the insert. If duplicate data is found inserted within just seconds (or minutes. whatever makes most sense in your situation), show warning message and have user hit submit again if user feels it is not error. (This method makes most sense when you have user account and user is submitting data when logged in, so duplicate data check is done for the user.)
查看更多
登录 后发表回答