Avoid duplication of postback in ASP.Net (C #)

Simple here ... is there a clean way to prevent the user from double-clicking a button in a web form and thereby causing duplication of events?

If I had, for example, a comment form, and the user entered “this is my comment” and clicks the “Send” button, the comment is displayed below ... however, if they double-click, triple-click or the keyboard just goes crazy, they can release several versions.

On the client side, I could easily disable the onclick button - but I prefer server solutions for such things :)

Is there a timeout after return for one viewport that can be set, for example?

thanks

+7
c # postback
source share
7 answers

I do not think you should load the server for such trivial tasks. You can try something like jQuery UI like this . The Microsoft Ajax toolkit should also have some control that does the same. I used it a long time ago, but I don’t seem to remember the control name.

+2
source share

With jQuery you can use the one event.

Another interesting point: Create your ASP.NET pages on the richer Bedrock .

+2
source share

Set the session variable when the user enters this page, for example Session ["FormXYZSubmitted"] = false. When the form is submitted, check that the type variable

if((bool) Session["FormXYZSubmitted"] == false) { // save to db Session["FormXYZSubmitted"] = true; } 
+1
source share

I had the same scenario. The decision of one of my colleagues was to implement a kind of timer in Javascript in order to avoid a second-click.

Hope this helps,

0
source share

Disable button on click, use jquery or ajax toolkit for Microsoft.

0
source share

Depending on how important this is for you, you can create an array of a one-time GUID that you remove from the array after the update has been processed (i.e. sent back to the viewstate / hidden field)

If the pointer is not in the array during postback, the request is invalid.

Replace the database table for the array in a clustered environment.

0
source share

The client side can be complicated if you use Asp.Net validation.

If you have a homepage, put it on your homepage:

  void IterateThroughControls(Control parent) { foreach (Control SelectedButton in parent.Controls) { if (SelectedButton is Button) { ((Button)SelectedButton).Attributes.Add("onclick", " this.disabled = true; " + Page.ClientScript.GetPostBackEventReference(((Button)SelectedButton), null) + ";"); } if (SelectedButton.Controls.Count > 0) { IterateThroughControls(SelectedButton); } } } 

Then add this to the main page of Page_Load:

 IterateThroughControls(this); 
0
source share

All Articles