7

I have following code inside a button click handler. Both approaches work fine. The Page_ClientValidate() causes an extra validation check and do processing whereas Page_IsValid makes use of existing property.

QUESTIONS

  1. In case of button click, is it always guaranteed that Page_IsValid would have been calculated by that time? If is not guaranteed, we need to call Page_ClientValidate() explicitly.
  2. What are the events that happen before Page_IsValid is set? For such events we should not rely on Page_IsValid

UPDATE

Is it assured that the button click event handler (in JavaScript) will be called only after the validation part is completed (i.e., after Page_ClientValidate() was invoked as part of validation) ? If this is assured, can I rely on Page_IsValid?

SCRIPT

  $('#btnSave').click(function (e) {

  //Aproach 1
  var isValid = Page_ClientValidate('');
  if (isValid) 
  {
       //Do reamining work
  }

  //Aproach 2
  if (Page_IsValid)
  {
      //Do reamining work
  }

  });

REFERENCES:

  1. Hide redundant error message in ASP.Net ValidationSummary
  2. Validator causes improper behavior for double click check
  3. Page_ClientValidate is not defined
  4. Page_ClientValidate is validating multiple times.
  5. MSDN - ASP.NET Validation in Depth
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 1
    This Page_IsValid never worked for me in javascript. I always use Page_ClientValidate in javascript and Page_IsValid in server side code – Moons Dec 12 '12 at 04:39
  • 1
    @Moons have you ever posted the issue in stack overflow - "Page_IsValid never worked for me in javascript" ? – LCJ Dec 12 '12 at 04:40
  • Actually not. I tried once using Page_IsValid and it didnt worked and then i started using Page_ClientValidate – Moons Dec 12 '12 at 04:41
  • 2
    Page_IsValid sets in ValidatorUpdateIsValid() which is inside Page_ClientValidate() and Page_ClientValidate() is inside WebForm_DoPostBackWithOptions(). Everytime when you do postback with standart control this event fires. Otherwise if you call for example __doPostBack() of course you have to call Page_ClientValidate() for validation. – Boriss Pavlovs Dec 12 '12 at 07:45
  • 2
    Page_ClientValidate() invokes ValidatorUpdateIsValid(), this function invokes AllValidatorsValid(Page_Validators) where Page_Validators array of validator objects. AllValidatorsValid(Page_Validators) checks isvalid property for every validator and return result (true or false) what assigns to the Page_IsValid Example: function ValidatorUpdateIsValid() { Page_IsValid = AllValidatorsValid(Page_Validators); – Boriss Pavlovs May 29 '13 at 06:18

1 Answers1

4
  1. In case of button click, Page_ClientValidate() is called when (and only when) the button's CausesValidation is set to true.

  2. Page_ClientValidate() is part of process of doing postback, so it is called within button's click.
    I rely on Page_IsValid only in a scope of a function after calling Page_ClientValidate(). Otherwise I always call Page_ClientValidate().

Comment: calling Page_ClientValidate() repeatedly may cause the page to be too obtrusive (multiple alerts etc.). That's why it's good to have a custom validate function that takes care of all validation.

Peter Ivan
  • 1,467
  • 2
  • 14
  • 27
  • Is it assured that the button click event handler (in JavaScript) will be called only after the validation part is completed (i.e., after Page_ClientValidate() was invoked as part of validation) ? If this is assured, I can rely on Page_IsValid. – LCJ May 23 '13 at 14:07
  • 1
    @Lijo: See my first point - it is assured only when the button's `CausesValidation` is set to `true`. – Peter Ivan May 24 '13 at 06:06