How to prevent retransmissions from confusing my business layer

I have a web application (ASP.Net 3.5) with a traditional three-layer design. If the user presses a button, a postback occurs, some middle code and data level code are executed, and the screen is updated. If the user presses the button several times before the completion of the first postback, my logic gets confused and the application may be in an invalid state. What are the best ways to prevent this?

I can use javascript to disable the button, but that just hides the problem. How can I create my own business and data layers for this?

+6
form-submit
source share
7 answers

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

+2
source share

If I were brutally honest, I would say that it looks like you are confused with web registries and not with your application (this is if you are the one who wrote it) .; -)

However, in addition to other suggestions, I would make in this case a โ€œtokenโ€ in a hidden field in the form - like a GUID - that was sent back. Use this to track work in progress and allow it to be used only once. For example. when sending back, put it in the session store. Each time a postback is performed, first check the session for this token, and if it is there, do nothing. If it is NOT there, save it in the session and do the work. When the session ends, the markers are automatically discarded. Easy. Much better than some kind of curved database token.

  • Oisin
+1
source share
  • After clicking the button, disable the submit button . This will prevent accidental double-clicking or more.
  • I usually redirect to another URL after the postback to avoid accidentally / intentionally refreshing the page.
  • Finally, in your DB insert method, verify that identical data is inserted within a certain time interval (possibly in seconds) before doing the insert. If you find duplicate data inserted within a few seconds (or minutes, which is most important in your situation), display a warning message and ask the user to send the message again if the user believes that this is not an error. (This method makes the most sense when you have a user account and the user sends data when they log in, so the data is checked again for the user.)
0
source share

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

0
source share

I solved the problem with a javascript entry disabling the button of the click function:

MyButton.Attributes.Add("onclick", "javascript:this.onclick=function(){return false;};"); 
0
source share

We all saw sites that disabled the submit buttons when you click on them. This is often done so that users do not click a button several times. This is usually done using the onclick JavaScript event to disable the button. In ASP.NET, each server-side element already has an onclick event handler, which calls the server to handle events. To accomplish the same thing in ASP.NET, you can easily do:

 btnSubmit.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnSubmit).ToString()); 

Where 'btnSubmit' is the name of this button. What happens here, we create an onclick event that does two things. Firstly, it disables the button in the users browser. The second thing he does is send a normal postback event to the server.

0
source share

Even I got the same problem as me, as below.

After downloading the file When redirecting to the same page or some other page in your project this problem can be avoided.

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 as me, as below.

After downloading the file If you are redirected to the same page or to another page of your project. After the redirect, the answer will not be there as soon as you are redirected.

In my aspx

In my code

 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 successful messages and error messages, try using sessions.

0
source share

All Articles