Programming Languages Hacks

Importanti regole per linguaggi di programmazione rilevanti come Java, C, C++, C#…

  • Subscribe

  • Lettori

    I miei lettori abituali

  • Twitter

Page Doesn’t Postback After Validation Error

Posted by Ricibald on June 27th, 2013

If there is a validation error, web controls that don’t need to repeat validation don’t postback on first attempt but postback on second attempt.

The reason (thanks to Lions Den) is that after running Page_ClientValidate ASP.NET runs another built in function ValidatorCommonOnSubmit. Within Page_ClientValidate, Page_BlockSubmit is set based on the validation. The postback is then blocked in ValidatorCommonOnSubmit if Page_BlockSubmit is true. No matter what, at the end of the function Page_BlockSubmit is always reset back to false. If a page does a postback without running any validation and Page_BlockSubmit has not been reset to false, the partial postback will be blocked.

First solution: reset Page_BlockSubmit

In this solution we just reset Page_BlockSubmit. This is the simplest solution and doesn’t have collateral effects. But you need to set this on every case you’ll need it.

function DoValidation() {
   var validated = Page_ClientValidate('ValidationGroup');

   // THIS IS THE CHANGE: REMOVE THE FLAG
   Page_BlockSubmit = false;

   return validated;
}

Second solution: never block postback

In this solution we redefine the code of ValidatorCommonOnSubmit returning always true (just make sure to copy and paste the same ASP.NET implementation):

function ValidatorCommonOnSubmit() {
   Page_InvalidControlToBeFocused = null;
   var result = !Page_BlockSubmit;
   if ((typeof(window.event) != "undefined") && (window.event != null)) {
      window.event.returnValue = result;
   }
   Page_BlockSubmit = false;

   // THIS IS THE CHANGE: ALWAYS RETURN TRUE
   return true;
}

Leave a Reply

You must be logged in to post a comment.