JavaScript click event collides with ASP validation

Asked 2 years ago, Updated 2 years ago, 154 views

ASP.NET supports double transmission, but the validation and JS click events collide, so I can't reach the event handler...

aspx

<asp: ImageButton ID="imgUpdate" runat="server" CauseValidation="false"
 OnClientClick="returnisValid(); 
 "ImageUrl="~/Images/Update 32x32.png" 
 Text="Update" ToolTip="Update" OnClick="imgUpdate_Click"/>

JavaScript

<script type="text/javascript">
function isValid() {
   $('form').submit(function(){      
       Page_ClientValidate();
       if(!Page_IsValid) return false;
    // double transmission prevention
    $(this).submit(function(){
        return false;
       });
   }); 
 }
</script>

Reference URL

Once the client submitted a validation error, corrected the error and pressed submit again, it was judged to be a double transmission.
(I understood that I could adjust the order of validation if I set CauseValidation="false", but it seems to be returned false in the process of preventing double transmission.)

After debugging, $(this).submit(function(){ has been accessed many times. Is there something wrong with the conditional branch?

javascript c# .net asp.net

2022-09-30 21:16

1 Answers

I don't know why the code in the questionnaire doesn't cause postbacks, but there are two problems, so I think it will work properly if you correct them.

First,

After debugging, $(this).submit(function(){ has been accessed many times. Is there something wrong with the conditional branch?

However, this is because each time isValid() runs, the function is bound to form.onsubmit.Therefore, if Page_IsValid does not reach true in one click, the functions registered in the previous click will also be called.

Also, the onsubmit event registered with jQuery always occurs after the onclick attribute WebForm_OnSubmit().Therefore, you do not need to bind with onclick.

For the above reasons,

without specifying CauseValidation
<script type="text/javascript">
    $('form').submit(function(){
        if(Page_IsValid)
            $(this).submit(function(){
                return false;
            });
    });
</script>

If you run a script like the one shown in at load, it prevents you from pressing it twice.


2022-09-30 21:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.