TheJediCowboy
TheJediCowboy

Reputation: 9222

Preventing Over Posting in ASP.NET Webforms

I know this is a basic question, but I am curious as to what the different options are, and what the best practice would be.

If I have a form that is responsible for saving reservations into a system, how can I prevent the form from being posted twice if the user hits the button twice really quickly?

I know there are a few ways in which I can accomplish this, but I am not quite sure which is the standard way of preventing this. Partially because I am new to web forms, and am used to dealing with MVC.

Thanks ahead of time.

Upvotes: 4

Views: 781

Answers (5)

Maslow
Maslow

Reputation: 18746

 <asp:Button ID="CopyScenarioButton" ClientIDMode="Static" OnClick="CopyScenarioButton_Click"
                            OnClientClick="setTimeout( function() {$('#CopyScenarioButton').attr('disabled', 'disabled');},0)"
                            EnableViewState="false" Text="Save New Scenario" ToolTip="Save New Scenario"
                            CssClass="btnNormal" runat="server" />

or the later version that includes some validation first:

    function PreSaveHybrid() {
       var doSave = PreSave();
       if (doSave !== false) //returns nothing if it's not a cancel
       setTimeout(function () { $('#btnSave').attr('disabled', 'disabled'); }, 0);
    return doSave;
}

Upvotes: 0

hmqcnoesy
hmqcnoesy

Reputation: 4225

I have been using something like this when using an asp:Button for submitting:

1) Set the submit button's UseSubmitBehavior="false"

2) Set the submit button's OnClientClick="pleaseWait(this, 'Please Wait...');"

3) Include javascript code in the page:

function pleaseWait(obj, message) {
    if (typeof(Page_ClientValidate) == 'function') {
        if (Page_ClientValidate()) {
            obj.disabled = true;
            obj.value = message;
            return true;
        }
    }
    return false;
} 

This solution is nice because it is simple but still accounts for client-side javascript validations. It isn't perfect, because it still relies on Javascript, which could be turned off, but that's unlikely to be done by someone who doesn't have the sense to click once and wait for a response. :)

Upvotes: 1

Joshua
Joshua

Reputation: 8212

I've used two approaches to this problem:

  1. Use a token based approach. Each page has a hidden input with the current random token. This token is also stored in the user's session. Once the postback occurrs, I compare tokens and, if they are valid, generate a new session token and continue processing. When the second postback occurs, the token no longer matches and prevents processing.

  2. Use javascript to disable the submit button. If you take this approach, and need the button event handler to fire, you'll need to create a hidden input with the name attribute of the button before submitting. The hidden input is required because disabled inputs do not end up in the post data.

Upvotes: 2

Glenn Ferrie
Glenn Ferrie

Reputation: 10390

I would recommend a client-side onClick event handler that disables the button or makes it invisible, preferably the latter, and replace the button with a label that reads "Processing..." or something like this

Upvotes: 1

drdwilcox
drdwilcox

Reputation: 3951

Easy way - use the ajax AnimationExtender control.http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/Animation/Animation.aspx

Simply attach the extender to the button and add a disable action.

Upvotes: 0

Related Questions