ASP.Net double-click problem

前端 未结 6 1836
孤城傲影
孤城傲影 2020-12-02 17:27

having a slight problem with an ASP.net page of mine. If a user were to double click on a \"submit\" button it will write to the database twice (i.e. carry out the \'onclick

6条回答
  •  没有蜡笔的小新
    2020-12-02 17:58

    The general approach is twofold.

    Serverside:

    1. On load of the page, generate a token (using System.Random), save it in the session, and write it to a hidden form field
    2. On submit, check that the hidden form field equals the session variable (before setting it again)
    3. Do work

    Clientside:

    Similar to what you have, but probably just hide the button, and replace it with some text like 'submitting'.

    The important thing to note, client side, is that the user may cancel the post by hitting 'escape', so you should consider what to do here (depending on how far along they are the token won't be used, so you'll need to bring the button back from being disabled/hidden).

    Complete example follows:

    C# (includes code to see it in action):

    
    
        double-click test
        
    
    
        
        
    Result: 

提交回复
热议问题