简体   繁体   中英

Limiting ASP.NET webforms OnClick event to one time

I have a Web forms button like this which serves to complete the order that user has made on the website using paypal:

<asp:Button style="padding-left:10px !important;" ID="Button1" runat="server" OnClick="Button1_Click" Text="Complete order" />

Once the button is clicked... the data of the order gets stored inside the DB. Now my problem is, what if the user triggers the event multiple times at once (for no reason lets say)... If they do that.. I'll get multiple errors... So my question here is how do I limit the user to only be able to trigger the on click event just once... Next time he/she tries to click it, the event won't be triggered? Is there any handy way to do this via Server/Client side programming? Perhaps via Javascript??

You can do this client side with JQuery via " one "

$('#SomeForm').one('submit', function() {
    $(this).find('input[type="submit"]').attr('disabled','disabled');
});

That will only let the button be clicked once. You shoudln't rely on this for payment scenarios tho. You can tag something into Session["Clicked"] = true; or similar to make sure that it only gets processed once server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM